os_windows.cpp revision 20:e195fe4c40c7
1/*
2 * Copyright 1997-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25#ifdef _WIN64
26// Must be at least Windows 2000 or XP to use VectoredExceptions
27#define _WIN32_WINNT 0x500
28#endif
29
30// do not include precompiled header file
31# include "incls/_os_windows.cpp.incl"
32
33#ifdef _DEBUG
34#include <crtdbg.h>
35#endif
36
37
38#include <windows.h>
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <sys/timeb.h>
42#include <objidl.h>
43#include <shlobj.h>
44
45#include <malloc.h>
46#include <signal.h>
47#include <direct.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <io.h>
51#include <process.h>              // For _beginthreadex(), _endthreadex()
52#include <imagehlp.h>             // For os::dll_address_to_function_name
53
54/* for enumerating dll libraries */
55#include <tlhelp32.h>
56#include <vdmdbg.h>
57
58// for timer info max values which include all bits
59#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
60
61// For DLL loading/load error detection
62// Values of PE COFF
63#define IMAGE_FILE_PTR_TO_SIGNATURE 0x3c
64#define IMAGE_FILE_SIGNATURE_LENGTH 4
65
66static HANDLE main_process;
67static HANDLE main_thread;
68static int    main_thread_id;
69
70static FILETIME process_creation_time;
71static FILETIME process_exit_time;
72static FILETIME process_user_time;
73static FILETIME process_kernel_time;
74
75#ifdef _WIN64
76PVOID  topLevelVectoredExceptionHandler = NULL;
77#endif
78
79#ifdef _M_IA64
80#define __CPU__ ia64
81#elif _M_AMD64
82#define __CPU__ amd64
83#else
84#define __CPU__ i486
85#endif
86
87// save DLL module handle, used by GetModuleFileName
88
89HINSTANCE vm_lib_handle;
90static int getLastErrorString(char *buf, size_t len);
91
92BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
93  switch (reason) {
94    case DLL_PROCESS_ATTACH:
95      vm_lib_handle = hinst;
96      if(ForceTimeHighResolution)
97        timeBeginPeriod(1L);
98      break;
99    case DLL_PROCESS_DETACH:
100      if(ForceTimeHighResolution)
101        timeEndPeriod(1L);
102#ifdef _WIN64
103      if (topLevelVectoredExceptionHandler != NULL) {
104        RemoveVectoredExceptionHandler(topLevelVectoredExceptionHandler);
105        topLevelVectoredExceptionHandler = NULL;
106      }
107#endif
108      break;
109    default:
110      break;
111  }
112  return true;
113}
114
115static inline double fileTimeAsDouble(FILETIME* time) {
116  const double high  = (double) ((unsigned int) ~0);
117  const double split = 10000000.0;
118  double result = (time->dwLowDateTime / split) +
119                   time->dwHighDateTime * (high/split);
120  return result;
121}
122
123// Implementation of os
124
125bool os::getenv(const char* name, char* buffer, int len) {
126 int result = GetEnvironmentVariable(name, buffer, len);
127 return result > 0 && result < len;
128}
129
130
131// No setuid programs under Windows.
132bool os::have_special_privileges() {
133  return false;
134}
135
136
137// This method is  a periodic task to check for misbehaving JNI applications
138// under CheckJNI, we can add any periodic checks here.
139// For Windows at the moment does nothing
140void os::run_periodic_checks() {
141  return;
142}
143
144#ifndef _WIN64
145LONG WINAPI Handle_FLT_Exception(struct _EXCEPTION_POINTERS* exceptionInfo);
146#endif
147void os::init_system_properties_values() {
148  /* sysclasspath, java_home, dll_dir */
149  {
150      char *home_path;
151      char *dll_path;
152      char *pslash;
153      char *bin = "\\bin";
154      char home_dir[MAX_PATH];
155
156      if (!getenv("_ALT_JAVA_HOME_DIR", home_dir, MAX_PATH)) {
157          os::jvm_path(home_dir, sizeof(home_dir));
158          // Found the full path to jvm[_g].dll.
159          // Now cut the path to <java_home>/jre if we can.
160          *(strrchr(home_dir, '\\')) = '\0';  /* get rid of \jvm.dll */
161          pslash = strrchr(home_dir, '\\');
162          if (pslash != NULL) {
163              *pslash = '\0';                 /* get rid of \{client|server} */
164              pslash = strrchr(home_dir, '\\');
165              if (pslash != NULL)
166                  *pslash = '\0';             /* get rid of \bin */
167          }
168      }
169
170      home_path = NEW_C_HEAP_ARRAY(char, strlen(home_dir) + 1);
171      if (home_path == NULL)
172          return;
173      strcpy(home_path, home_dir);
174      Arguments::set_java_home(home_path);
175
176      dll_path = NEW_C_HEAP_ARRAY(char, strlen(home_dir) + strlen(bin) + 1);
177      if (dll_path == NULL)
178          return;
179      strcpy(dll_path, home_dir);
180      strcat(dll_path, bin);
181      Arguments::set_dll_dir(dll_path);
182
183      if (!set_boot_path('\\', ';'))
184          return;
185  }
186
187  /* library_path */
188  #define EXT_DIR "\\lib\\ext"
189  #define BIN_DIR "\\bin"
190  #define PACKAGE_DIR "\\Sun\\Java"
191  {
192    /* Win32 library search order (See the documentation for LoadLibrary):
193     *
194     * 1. The directory from which application is loaded.
195     * 2. The current directory
196     * 3. The system wide Java Extensions directory (Java only)
197     * 4. System directory (GetSystemDirectory)
198     * 5. Windows directory (GetWindowsDirectory)
199     * 6. The PATH environment variable
200     */
201
202    char *library_path;
203    char tmp[MAX_PATH];
204    char *path_str = ::getenv("PATH");
205
206    library_path = NEW_C_HEAP_ARRAY(char, MAX_PATH * 5 + sizeof(PACKAGE_DIR) +
207        sizeof(BIN_DIR) + (path_str ? strlen(path_str) : 0) + 10);
208
209    library_path[0] = '\0';
210
211    GetModuleFileName(NULL, tmp, sizeof(tmp));
212    *(strrchr(tmp, '\\')) = '\0';
213    strcat(library_path, tmp);
214
215    strcat(library_path, ";.");
216
217    GetWindowsDirectory(tmp, sizeof(tmp));
218    strcat(library_path, ";");
219    strcat(library_path, tmp);
220    strcat(library_path, PACKAGE_DIR BIN_DIR);
221
222    GetSystemDirectory(tmp, sizeof(tmp));
223    strcat(library_path, ";");
224    strcat(library_path, tmp);
225
226    GetWindowsDirectory(tmp, sizeof(tmp));
227    strcat(library_path, ";");
228    strcat(library_path, tmp);
229
230    if (path_str) {
231        strcat(library_path, ";");
232        strcat(library_path, path_str);
233    }
234
235    Arguments::set_library_path(library_path);
236    FREE_C_HEAP_ARRAY(char, library_path);
237  }
238
239  /* Default extensions directory */
240  {
241    char path[MAX_PATH];
242    char buf[2 * MAX_PATH + 2 * sizeof(EXT_DIR) + sizeof(PACKAGE_DIR) + 1];
243    GetWindowsDirectory(path, MAX_PATH);
244    sprintf(buf, "%s%s;%s%s%s", Arguments::get_java_home(), EXT_DIR,
245        path, PACKAGE_DIR, EXT_DIR);
246    Arguments::set_ext_dirs(buf);
247  }
248  #undef EXT_DIR
249  #undef BIN_DIR
250  #undef PACKAGE_DIR
251
252  /* Default endorsed standards directory. */
253  {
254    #define ENDORSED_DIR "\\lib\\endorsed"
255    size_t len = strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR);
256    char * buf = NEW_C_HEAP_ARRAY(char, len);
257    sprintf(buf, "%s%s", Arguments::get_java_home(), ENDORSED_DIR);
258    Arguments::set_endorsed_dirs(buf);
259    #undef ENDORSED_DIR
260  }
261
262#ifndef _WIN64
263  SetUnhandledExceptionFilter(Handle_FLT_Exception);
264#endif
265
266  // Done
267  return;
268}
269
270void os::breakpoint() {
271  DebugBreak();
272}
273
274// Invoked from the BREAKPOINT Macro
275extern "C" void breakpoint() {
276  os::breakpoint();
277}
278
279// Returns an estimate of the current stack pointer. Result must be guaranteed
280// to point into the calling threads stack, and be no lower than the current
281// stack pointer.
282
283address os::current_stack_pointer() {
284  int dummy;
285  address sp = (address)&dummy;
286  return sp;
287}
288
289// os::current_stack_base()
290//
291//   Returns the base of the stack, which is the stack's
292//   starting address.  This function must be called
293//   while running on the stack of the thread being queried.
294
295address os::current_stack_base() {
296  MEMORY_BASIC_INFORMATION minfo;
297  address stack_bottom;
298  size_t stack_size;
299
300  VirtualQuery(&minfo, &minfo, sizeof(minfo));
301  stack_bottom =  (address)minfo.AllocationBase;
302  stack_size = minfo.RegionSize;
303
304  // Add up the sizes of all the regions with the same
305  // AllocationBase.
306  while( 1 )
307  {
308    VirtualQuery(stack_bottom+stack_size, &minfo, sizeof(minfo));
309    if ( stack_bottom == (address)minfo.AllocationBase )
310      stack_size += minfo.RegionSize;
311    else
312      break;
313  }
314
315#ifdef _M_IA64
316  // IA64 has memory and register stacks
317  stack_size = stack_size / 2;
318#endif
319  return stack_bottom + stack_size;
320}
321
322size_t os::current_stack_size() {
323  size_t sz;
324  MEMORY_BASIC_INFORMATION minfo;
325  VirtualQuery(&minfo, &minfo, sizeof(minfo));
326  sz = (size_t)os::current_stack_base() - (size_t)minfo.AllocationBase;
327  return sz;
328}
329
330
331LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo);
332
333// Thread start routine for all new Java threads
334static unsigned __stdcall java_start(Thread* thread) {
335  // Try to randomize the cache line index of hot stack frames.
336  // This helps when threads of the same stack traces evict each other's
337  // cache lines. The threads can be either from the same JVM instance, or
338  // from different JVM instances. The benefit is especially true for
339  // processors with hyperthreading technology.
340  static int counter = 0;
341  int pid = os::current_process_id();
342  _alloca(((pid ^ counter++) & 7) * 128);
343
344  OSThread* osthr = thread->osthread();
345  assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
346
347  if (UseNUMA) {
348    int lgrp_id = os::numa_get_group_id();
349    if (lgrp_id != -1) {
350      thread->set_lgrp_id(lgrp_id);
351    }
352  }
353
354
355  if (UseVectoredExceptions) {
356    // If we are using vectored exception we don't need to set a SEH
357    thread->run();
358  }
359  else {
360    // Install a win32 structured exception handler around every thread created
361    // by VM, so VM can genrate error dump when an exception occurred in non-
362    // Java thread (e.g. VM thread).
363    __try {
364       thread->run();
365    } __except(topLevelExceptionFilter(
366               (_EXCEPTION_POINTERS*)_exception_info())) {
367        // Nothing to do.
368    }
369  }
370
371  // One less thread is executing
372  // When the VMThread gets here, the main thread may have already exited
373  // which frees the CodeHeap containing the Atomic::add code
374  if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
375    Atomic::dec_ptr((intptr_t*)&os::win32::_os_thread_count);
376  }
377
378  return 0;
379}
380
381static OSThread* create_os_thread(Thread* thread, HANDLE thread_handle, int thread_id) {
382  // Allocate the OSThread object
383  OSThread* osthread = new OSThread(NULL, NULL);
384  if (osthread == NULL) return NULL;
385
386  // Initialize support for Java interrupts
387  HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
388  if (interrupt_event == NULL) {
389    delete osthread;
390    return NULL;
391  }
392  osthread->set_interrupt_event(interrupt_event);
393
394  // Store info on the Win32 thread into the OSThread
395  osthread->set_thread_handle(thread_handle);
396  osthread->set_thread_id(thread_id);
397
398  if (UseNUMA) {
399    int lgrp_id = os::numa_get_group_id();
400    if (lgrp_id != -1) {
401      thread->set_lgrp_id(lgrp_id);
402    }
403  }
404
405  // Initial thread state is INITIALIZED, not SUSPENDED
406  osthread->set_state(INITIALIZED);
407
408  return osthread;
409}
410
411
412bool os::create_attached_thread(JavaThread* thread) {
413#ifdef ASSERT
414  thread->verify_not_published();
415#endif
416  HANDLE thread_h;
417  if (!DuplicateHandle(main_process, GetCurrentThread(), GetCurrentProcess(),
418                       &thread_h, THREAD_ALL_ACCESS, false, 0)) {
419    fatal("DuplicateHandle failed\n");
420  }
421  OSThread* osthread = create_os_thread(thread, thread_h,
422                                        (int)current_thread_id());
423  if (osthread == NULL) {
424     return false;
425  }
426
427  // Initial thread state is RUNNABLE
428  osthread->set_state(RUNNABLE);
429
430  thread->set_osthread(osthread);
431  return true;
432}
433
434bool os::create_main_thread(JavaThread* thread) {
435#ifdef ASSERT
436  thread->verify_not_published();
437#endif
438  if (_starting_thread == NULL) {
439    _starting_thread = create_os_thread(thread, main_thread, main_thread_id);
440     if (_starting_thread == NULL) {
441        return false;
442     }
443  }
444
445  // The primordial thread is runnable from the start)
446  _starting_thread->set_state(RUNNABLE);
447
448  thread->set_osthread(_starting_thread);
449  return true;
450}
451
452// Allocate and initialize a new OSThread
453bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
454  unsigned thread_id;
455
456  // Allocate the OSThread object
457  OSThread* osthread = new OSThread(NULL, NULL);
458  if (osthread == NULL) {
459    return false;
460  }
461
462  // Initialize support for Java interrupts
463  HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
464  if (interrupt_event == NULL) {
465    delete osthread;
466    return NULL;
467  }
468  osthread->set_interrupt_event(interrupt_event);
469  osthread->set_interrupted(false);
470
471  thread->set_osthread(osthread);
472
473  if (stack_size == 0) {
474    switch (thr_type) {
475    case os::java_thread:
476      // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
477      if (JavaThread::stack_size_at_create() > 0)
478        stack_size = JavaThread::stack_size_at_create();
479      break;
480    case os::compiler_thread:
481      if (CompilerThreadStackSize > 0) {
482        stack_size = (size_t)(CompilerThreadStackSize * K);
483        break;
484      } // else fall through:
485        // use VMThreadStackSize if CompilerThreadStackSize is not defined
486    case os::vm_thread:
487    case os::pgc_thread:
488    case os::cgc_thread:
489    case os::watcher_thread:
490      if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
491      break;
492    }
493  }
494
495  // Create the Win32 thread
496  //
497  // Contrary to what MSDN document says, "stack_size" in _beginthreadex()
498  // does not specify stack size. Instead, it specifies the size of
499  // initially committed space. The stack size is determined by
500  // PE header in the executable. If the committed "stack_size" is larger
501  // than default value in the PE header, the stack is rounded up to the
502  // nearest multiple of 1MB. For example if the launcher has default
503  // stack size of 320k, specifying any size less than 320k does not
504  // affect the actual stack size at all, it only affects the initial
505  // commitment. On the other hand, specifying 'stack_size' larger than
506  // default value may cause significant increase in memory usage, because
507  // not only the stack space will be rounded up to MB, but also the
508  // entire space is committed upfront.
509  //
510  // Finally Windows XP added a new flag 'STACK_SIZE_PARAM_IS_A_RESERVATION'
511  // for CreateThread() that can treat 'stack_size' as stack size. However we
512  // are not supposed to call CreateThread() directly according to MSDN
513  // document because JVM uses C runtime library. The good news is that the
514  // flag appears to work with _beginthredex() as well.
515
516#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
517#define STACK_SIZE_PARAM_IS_A_RESERVATION  (0x10000)
518#endif
519
520  HANDLE thread_handle =
521    (HANDLE)_beginthreadex(NULL,
522                           (unsigned)stack_size,
523                           (unsigned (__stdcall *)(void*)) java_start,
524                           thread,
525                           CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
526                           &thread_id);
527  if (thread_handle == NULL) {
528    // perhaps STACK_SIZE_PARAM_IS_A_RESERVATION is not supported, try again
529    // without the flag.
530    thread_handle =
531    (HANDLE)_beginthreadex(NULL,
532                           (unsigned)stack_size,
533                           (unsigned (__stdcall *)(void*)) java_start,
534                           thread,
535                           CREATE_SUSPENDED,
536                           &thread_id);
537  }
538  if (thread_handle == NULL) {
539    // Need to clean up stuff we've allocated so far
540    CloseHandle(osthread->interrupt_event());
541    thread->set_osthread(NULL);
542    delete osthread;
543    return NULL;
544  }
545
546  Atomic::inc_ptr((intptr_t*)&os::win32::_os_thread_count);
547
548  // Store info on the Win32 thread into the OSThread
549  osthread->set_thread_handle(thread_handle);
550  osthread->set_thread_id(thread_id);
551
552  // Initial thread state is INITIALIZED, not SUSPENDED
553  osthread->set_state(INITIALIZED);
554
555  // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
556  return true;
557}
558
559
560// Free Win32 resources related to the OSThread
561void os::free_thread(OSThread* osthread) {
562  assert(osthread != NULL, "osthread not set");
563  CloseHandle(osthread->thread_handle());
564  CloseHandle(osthread->interrupt_event());
565  delete osthread;
566}
567
568
569static int    has_performance_count = 0;
570static jlong first_filetime;
571static jlong initial_performance_count;
572static jlong performance_frequency;
573
574
575jlong as_long(LARGE_INTEGER x) {
576  jlong result = 0; // initialization to avoid warning
577  set_high(&result, x.HighPart);
578  set_low(&result,  x.LowPart);
579  return result;
580}
581
582
583jlong os::elapsed_counter() {
584  LARGE_INTEGER count;
585  if (has_performance_count) {
586    QueryPerformanceCounter(&count);
587    return as_long(count) - initial_performance_count;
588  } else {
589    FILETIME wt;
590    GetSystemTimeAsFileTime(&wt);
591    return (jlong_from(wt.dwHighDateTime, wt.dwLowDateTime) - first_filetime);
592  }
593}
594
595
596jlong os::elapsed_frequency() {
597  if (has_performance_count) {
598    return performance_frequency;
599  } else {
600   // the FILETIME time is the number of 100-nanosecond intervals since January 1,1601.
601   return 10000000;
602  }
603}
604
605
606julong os::available_memory() {
607  return win32::available_memory();
608}
609
610julong os::win32::available_memory() {
611  // FIXME: GlobalMemoryStatus() may return incorrect value if total memory
612  // is larger than 4GB
613  MEMORYSTATUS ms;
614  GlobalMemoryStatus(&ms);
615
616  return (julong)ms.dwAvailPhys;
617}
618
619julong os::physical_memory() {
620  return win32::physical_memory();
621}
622
623julong os::allocatable_physical_memory(julong size) {
624#ifdef _LP64
625  return size;
626#else
627  // Limit to 1400m because of the 2gb address space wall
628  return MIN2(size, (julong)1400*M);
629#endif
630}
631
632// VC6 lacks DWORD_PTR
633#if _MSC_VER < 1300
634typedef UINT_PTR DWORD_PTR;
635#endif
636
637int os::active_processor_count() {
638  DWORD_PTR lpProcessAffinityMask = 0;
639  DWORD_PTR lpSystemAffinityMask = 0;
640  int proc_count = processor_count();
641  if (proc_count <= sizeof(UINT_PTR) * BitsPerByte &&
642      GetProcessAffinityMask(GetCurrentProcess(), &lpProcessAffinityMask, &lpSystemAffinityMask)) {
643    // Nof active processors is number of bits in process affinity mask
644    int bitcount = 0;
645    while (lpProcessAffinityMask != 0) {
646      lpProcessAffinityMask = lpProcessAffinityMask & (lpProcessAffinityMask-1);
647      bitcount++;
648    }
649    return bitcount;
650  } else {
651    return proc_count;
652  }
653}
654
655bool os::distribute_processes(uint length, uint* distribution) {
656  // Not yet implemented.
657  return false;
658}
659
660bool os::bind_to_processor(uint processor_id) {
661  // Not yet implemented.
662  return false;
663}
664
665static void initialize_performance_counter() {
666  LARGE_INTEGER count;
667  if (QueryPerformanceFrequency(&count)) {
668    has_performance_count = 1;
669    performance_frequency = as_long(count);
670    QueryPerformanceCounter(&count);
671    initial_performance_count = as_long(count);
672  } else {
673    has_performance_count = 0;
674    FILETIME wt;
675    GetSystemTimeAsFileTime(&wt);
676    first_filetime = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
677  }
678}
679
680
681double os::elapsedTime() {
682  return (double) elapsed_counter() / (double) elapsed_frequency();
683}
684
685
686// Windows format:
687//   The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
688// Java format:
689//   Java standards require the number of milliseconds since 1/1/1970
690
691// Constant offset - calculated using offset()
692static jlong  _offset   = 116444736000000000;
693// Fake time counter for reproducible results when debugging
694static jlong  fake_time = 0;
695
696#ifdef ASSERT
697// Just to be safe, recalculate the offset in debug mode
698static jlong _calculated_offset = 0;
699static int   _has_calculated_offset = 0;
700
701jlong offset() {
702  if (_has_calculated_offset) return _calculated_offset;
703  SYSTEMTIME java_origin;
704  java_origin.wYear          = 1970;
705  java_origin.wMonth         = 1;
706  java_origin.wDayOfWeek     = 0; // ignored
707  java_origin.wDay           = 1;
708  java_origin.wHour          = 0;
709  java_origin.wMinute        = 0;
710  java_origin.wSecond        = 0;
711  java_origin.wMilliseconds  = 0;
712  FILETIME jot;
713  if (!SystemTimeToFileTime(&java_origin, &jot)) {
714    fatal1("Error = %d\nWindows error", GetLastError());
715  }
716  _calculated_offset = jlong_from(jot.dwHighDateTime, jot.dwLowDateTime);
717  _has_calculated_offset = 1;
718  assert(_calculated_offset == _offset, "Calculated and constant time offsets must be equal");
719  return _calculated_offset;
720}
721#else
722jlong offset() {
723  return _offset;
724}
725#endif
726
727jlong windows_to_java_time(FILETIME wt) {
728  jlong a = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
729  return (a - offset()) / 10000;
730}
731
732FILETIME java_to_windows_time(jlong l) {
733  jlong a = (l * 10000) + offset();
734  FILETIME result;
735  result.dwHighDateTime = high(a);
736  result.dwLowDateTime  = low(a);
737  return result;
738}
739
740jlong os::timeofday() {
741  FILETIME wt;
742  GetSystemTimeAsFileTime(&wt);
743  return windows_to_java_time(wt);
744}
745
746
747// Must return millis since Jan 1 1970 for JVM_CurrentTimeMillis
748// _use_global_time is only set if CacheTimeMillis is true
749jlong os::javaTimeMillis() {
750  if (UseFakeTimers) {
751    return fake_time++;
752  } else {
753    return (_use_global_time ? read_global_time() : timeofday());
754  }
755}
756
757#define NANOS_PER_SEC         CONST64(1000000000)
758#define NANOS_PER_MILLISEC    1000000
759jlong os::javaTimeNanos() {
760  if (!has_performance_count) {
761    return javaTimeMillis() * NANOS_PER_MILLISEC; // the best we can do.
762  } else {
763    LARGE_INTEGER current_count;
764    QueryPerformanceCounter(&current_count);
765    double current = as_long(current_count);
766    double freq = performance_frequency;
767    jlong time = (jlong)((current/freq) * NANOS_PER_SEC);
768    return time;
769  }
770}
771
772void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
773  if (!has_performance_count) {
774    // javaTimeMillis() doesn't have much percision,
775    // but it is not going to wrap -- so all 64 bits
776    info_ptr->max_value = ALL_64_BITS;
777
778    // this is a wall clock timer, so may skip
779    info_ptr->may_skip_backward = true;
780    info_ptr->may_skip_forward = true;
781  } else {
782    jlong freq = performance_frequency;
783    if (freq < NANOS_PER_SEC) {
784      // the performance counter is 64 bits and we will
785      // be multiplying it -- so no wrap in 64 bits
786      info_ptr->max_value = ALL_64_BITS;
787    } else if (freq > NANOS_PER_SEC) {
788      // use the max value the counter can reach to
789      // determine the max value which could be returned
790      julong max_counter = (julong)ALL_64_BITS;
791      info_ptr->max_value = (jlong)(max_counter / (freq / NANOS_PER_SEC));
792    } else {
793      // the performance counter is 64 bits and we will
794      // be using it directly -- so no wrap in 64 bits
795      info_ptr->max_value = ALL_64_BITS;
796    }
797
798    // using a counter, so no skipping
799    info_ptr->may_skip_backward = false;
800    info_ptr->may_skip_forward = false;
801  }
802  info_ptr->kind = JVMTI_TIMER_ELAPSED;                // elapsed not CPU time
803}
804
805char* os::local_time_string(char *buf, size_t buflen) {
806  SYSTEMTIME st;
807  GetLocalTime(&st);
808  jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
809               st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
810  return buf;
811}
812
813bool os::getTimesSecs(double* process_real_time,
814                     double* process_user_time,
815                     double* process_system_time) {
816  HANDLE h_process = GetCurrentProcess();
817  FILETIME create_time, exit_time, kernel_time, user_time;
818  BOOL result = GetProcessTimes(h_process,
819                               &create_time,
820                               &exit_time,
821                               &kernel_time,
822                               &user_time);
823  if (result != 0) {
824    FILETIME wt;
825    GetSystemTimeAsFileTime(&wt);
826    jlong rtc_millis = windows_to_java_time(wt);
827    jlong user_millis = windows_to_java_time(user_time);
828    jlong system_millis = windows_to_java_time(kernel_time);
829    *process_real_time = ((double) rtc_millis) / ((double) MILLIUNITS);
830    *process_user_time = ((double) user_millis) / ((double) MILLIUNITS);
831    *process_system_time = ((double) system_millis) / ((double) MILLIUNITS);
832    return true;
833  } else {
834    return false;
835  }
836}
837
838void os::shutdown() {
839
840  // allow PerfMemory to attempt cleanup of any persistent resources
841  perfMemory_exit();
842
843  // flush buffered output, finish log files
844  ostream_abort();
845
846  // Check for abort hook
847  abort_hook_t abort_hook = Arguments::abort_hook();
848  if (abort_hook != NULL) {
849    abort_hook();
850  }
851}
852
853void os::abort(bool dump_core)
854{
855  os::shutdown();
856  // no core dump on Windows
857  ::exit(1);
858}
859
860// Die immediately, no exit hook, no abort hook, no cleanup.
861void os::die() {
862  _exit(-1);
863}
864
865// Directory routines copied from src/win32/native/java/io/dirent_md.c
866//  * dirent_md.c       1.15 00/02/02
867//
868// The declarations for DIR and struct dirent are in jvm_win32.h.
869
870/* Caller must have already run dirname through JVM_NativePath, which removes
871   duplicate slashes and converts all instances of '/' into '\\'. */
872
873DIR *
874os::opendir(const char *dirname)
875{
876    assert(dirname != NULL, "just checking");   // hotspot change
877    DIR *dirp = (DIR *)malloc(sizeof(DIR));
878    DWORD fattr;                                // hotspot change
879    char alt_dirname[4] = { 0, 0, 0, 0 };
880
881    if (dirp == 0) {
882        errno = ENOMEM;
883        return 0;
884    }
885
886    /*
887     * Win32 accepts "\" in its POSIX stat(), but refuses to treat it
888     * as a directory in FindFirstFile().  We detect this case here and
889     * prepend the current drive name.
890     */
891    if (dirname[1] == '\0' && dirname[0] == '\\') {
892        alt_dirname[0] = _getdrive() + 'A' - 1;
893        alt_dirname[1] = ':';
894        alt_dirname[2] = '\\';
895        alt_dirname[3] = '\0';
896        dirname = alt_dirname;
897    }
898
899    dirp->path = (char *)malloc(strlen(dirname) + 5);
900    if (dirp->path == 0) {
901        free(dirp);
902        errno = ENOMEM;
903        return 0;
904    }
905    strcpy(dirp->path, dirname);
906
907    fattr = GetFileAttributes(dirp->path);
908    if (fattr == 0xffffffff) {
909        free(dirp->path);
910        free(dirp);
911        errno = ENOENT;
912        return 0;
913    } else if ((fattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
914        free(dirp->path);
915        free(dirp);
916        errno = ENOTDIR;
917        return 0;
918    }
919
920    /* Append "*.*", or possibly "\\*.*", to path */
921    if (dirp->path[1] == ':'
922        && (dirp->path[2] == '\0'
923            || (dirp->path[2] == '\\' && dirp->path[3] == '\0'))) {
924        /* No '\\' needed for cases like "Z:" or "Z:\" */
925        strcat(dirp->path, "*.*");
926    } else {
927        strcat(dirp->path, "\\*.*");
928    }
929
930    dirp->handle = FindFirstFile(dirp->path, &dirp->find_data);
931    if (dirp->handle == INVALID_HANDLE_VALUE) {
932        if (GetLastError() != ERROR_FILE_NOT_FOUND) {
933            free(dirp->path);
934            free(dirp);
935            errno = EACCES;
936            return 0;
937        }
938    }
939    return dirp;
940}
941
942/* parameter dbuf unused on Windows */
943
944struct dirent *
945os::readdir(DIR *dirp, dirent *dbuf)
946{
947    assert(dirp != NULL, "just checking");      // hotspot change
948    if (dirp->handle == INVALID_HANDLE_VALUE) {
949        return 0;
950    }
951
952    strcpy(dirp->dirent.d_name, dirp->find_data.cFileName);
953
954    if (!FindNextFile(dirp->handle, &dirp->find_data)) {
955        if (GetLastError() == ERROR_INVALID_HANDLE) {
956            errno = EBADF;
957            return 0;
958        }
959        FindClose(dirp->handle);
960        dirp->handle = INVALID_HANDLE_VALUE;
961    }
962
963    return &dirp->dirent;
964}
965
966int
967os::closedir(DIR *dirp)
968{
969    assert(dirp != NULL, "just checking");      // hotspot change
970    if (dirp->handle != INVALID_HANDLE_VALUE) {
971        if (!FindClose(dirp->handle)) {
972            errno = EBADF;
973            return -1;
974        }
975        dirp->handle = INVALID_HANDLE_VALUE;
976    }
977    free(dirp->path);
978    free(dirp);
979    return 0;
980}
981
982const char* os::dll_file_extension() { return ".dll"; }
983
984const char * os::get_temp_directory()
985{
986    static char path_buf[MAX_PATH];
987    if (GetTempPath(MAX_PATH, path_buf)>0)
988      return path_buf;
989    else{
990      path_buf[0]='\0';
991      return path_buf;
992    }
993}
994
995// Needs to be in os specific directory because windows requires another
996// header file <direct.h>
997const char* os::get_current_directory(char *buf, int buflen) {
998  return _getcwd(buf, buflen);
999}
1000
1001//-----------------------------------------------------------
1002// Helper functions for fatal error handler
1003
1004// The following library functions are resolved dynamically at runtime:
1005
1006// PSAPI functions, for Windows NT, 2000, XP
1007
1008// psapi.h doesn't come with Visual Studio 6; it can be downloaded as Platform
1009// SDK from Microsoft.  Here are the definitions copied from psapi.h
1010typedef struct _MODULEINFO {
1011    LPVOID lpBaseOfDll;
1012    DWORD SizeOfImage;
1013    LPVOID EntryPoint;
1014} MODULEINFO, *LPMODULEINFO;
1015
1016static BOOL  (WINAPI *_EnumProcessModules)  ( HANDLE, HMODULE *, DWORD, LPDWORD );
1017static DWORD (WINAPI *_GetModuleFileNameEx) ( HANDLE, HMODULE, LPTSTR, DWORD );
1018static BOOL  (WINAPI *_GetModuleInformation)( HANDLE, HMODULE, LPMODULEINFO, DWORD );
1019
1020// ToolHelp Functions, for Windows 95, 98 and ME
1021
1022static HANDLE(WINAPI *_CreateToolhelp32Snapshot)(DWORD,DWORD) ;
1023static BOOL  (WINAPI *_Module32First)           (HANDLE,LPMODULEENTRY32) ;
1024static BOOL  (WINAPI *_Module32Next)            (HANDLE,LPMODULEENTRY32) ;
1025
1026bool _has_psapi;
1027bool _psapi_init = false;
1028bool _has_toolhelp;
1029
1030static bool _init_psapi() {
1031  HINSTANCE psapi = LoadLibrary( "PSAPI.DLL" ) ;
1032  if( psapi == NULL ) return false ;
1033
1034  _EnumProcessModules = CAST_TO_FN_PTR(
1035      BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD),
1036      GetProcAddress(psapi, "EnumProcessModules")) ;
1037  _GetModuleFileNameEx = CAST_TO_FN_PTR(
1038      DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD),
1039      GetProcAddress(psapi, "GetModuleFileNameExA"));
1040  _GetModuleInformation = CAST_TO_FN_PTR(
1041      BOOL (WINAPI *)(HANDLE, HMODULE, LPMODULEINFO, DWORD),
1042      GetProcAddress(psapi, "GetModuleInformation"));
1043
1044  _has_psapi = (_EnumProcessModules && _GetModuleFileNameEx && _GetModuleInformation);
1045  _psapi_init = true;
1046  return _has_psapi;
1047}
1048
1049static bool _init_toolhelp() {
1050  HINSTANCE kernel32 = LoadLibrary("Kernel32.DLL") ;
1051  if (kernel32 == NULL) return false ;
1052
1053  _CreateToolhelp32Snapshot = CAST_TO_FN_PTR(
1054      HANDLE(WINAPI *)(DWORD,DWORD),
1055      GetProcAddress(kernel32, "CreateToolhelp32Snapshot"));
1056  _Module32First = CAST_TO_FN_PTR(
1057      BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1058      GetProcAddress(kernel32, "Module32First" ));
1059  _Module32Next = CAST_TO_FN_PTR(
1060      BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1061      GetProcAddress(kernel32, "Module32Next" ));
1062
1063  _has_toolhelp = (_CreateToolhelp32Snapshot && _Module32First && _Module32Next);
1064  return _has_toolhelp;
1065}
1066
1067#ifdef _WIN64
1068// Helper routine which returns true if address in
1069// within the NTDLL address space.
1070//
1071static bool _addr_in_ntdll( address addr )
1072{
1073  HMODULE hmod;
1074  MODULEINFO minfo;
1075
1076  hmod = GetModuleHandle("NTDLL.DLL");
1077  if ( hmod == NULL ) return false;
1078  if ( !_GetModuleInformation( GetCurrentProcess(), hmod,
1079                               &minfo, sizeof(MODULEINFO)) )
1080    return false;
1081
1082  if ( (addr >= minfo.lpBaseOfDll) &&
1083       (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage)))
1084    return true;
1085  else
1086    return false;
1087}
1088#endif
1089
1090
1091// Enumerate all modules for a given process ID
1092//
1093// Notice that Windows 95/98/Me and Windows NT/2000/XP have
1094// different API for doing this. We use PSAPI.DLL on NT based
1095// Windows and ToolHelp on 95/98/Me.
1096
1097// Callback function that is called by enumerate_modules() on
1098// every DLL module.
1099// Input parameters:
1100//    int       pid,
1101//    char*     module_file_name,
1102//    address   module_base_addr,
1103//    unsigned  module_size,
1104//    void*     param
1105typedef int (*EnumModulesCallbackFunc)(int, char *, address, unsigned, void *);
1106
1107// enumerate_modules for Windows NT, using PSAPI
1108static int _enumerate_modules_winnt( int pid, EnumModulesCallbackFunc func, void * param)
1109{
1110  HANDLE   hProcess ;
1111
1112# define MAX_NUM_MODULES 128
1113  HMODULE     modules[MAX_NUM_MODULES];
1114  static char filename[ MAX_PATH ];
1115  int         result = 0;
1116
1117  if (!_has_psapi && (_psapi_init || !_init_psapi())) return 0;
1118
1119  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
1120                         FALSE, pid ) ;
1121  if (hProcess == NULL) return 0;
1122
1123  DWORD size_needed;
1124  if (!_EnumProcessModules(hProcess, modules,
1125                           sizeof(modules), &size_needed)) {
1126      CloseHandle( hProcess );
1127      return 0;
1128  }
1129
1130  // number of modules that are currently loaded
1131  int num_modules = size_needed / sizeof(HMODULE);
1132
1133  for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
1134    // Get Full pathname:
1135    if(!_GetModuleFileNameEx(hProcess, modules[i],
1136                             filename, sizeof(filename))) {
1137        filename[0] = '\0';
1138    }
1139
1140    MODULEINFO modinfo;
1141    if (!_GetModuleInformation(hProcess, modules[i],
1142                               &modinfo, sizeof(modinfo))) {
1143        modinfo.lpBaseOfDll = NULL;
1144        modinfo.SizeOfImage = 0;
1145    }
1146
1147    // Invoke callback function
1148    result = func(pid, filename, (address)modinfo.lpBaseOfDll,
1149                  modinfo.SizeOfImage, param);
1150    if (result) break;
1151  }
1152
1153  CloseHandle( hProcess ) ;
1154  return result;
1155}
1156
1157
1158// enumerate_modules for Windows 95/98/ME, using TOOLHELP
1159static int _enumerate_modules_windows( int pid, EnumModulesCallbackFunc func, void *param)
1160{
1161  HANDLE                hSnapShot ;
1162  static MODULEENTRY32  modentry ;
1163  int                   result = 0;
1164
1165  if (!_has_toolhelp) return 0;
1166
1167  // Get a handle to a Toolhelp snapshot of the system
1168  hSnapShot = _CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ;
1169  if( hSnapShot == INVALID_HANDLE_VALUE ) {
1170      return FALSE ;
1171  }
1172
1173  // iterate through all modules
1174  modentry.dwSize = sizeof(MODULEENTRY32) ;
1175  bool not_done = _Module32First( hSnapShot, &modentry ) != 0;
1176
1177  while( not_done ) {
1178    // invoke the callback
1179    result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr,
1180                modentry.modBaseSize, param);
1181    if (result) break;
1182
1183    modentry.dwSize = sizeof(MODULEENTRY32) ;
1184    not_done = _Module32Next( hSnapShot, &modentry ) != 0;
1185  }
1186
1187  CloseHandle(hSnapShot);
1188  return result;
1189}
1190
1191int enumerate_modules( int pid, EnumModulesCallbackFunc func, void * param )
1192{
1193  // Get current process ID if caller doesn't provide it.
1194  if (!pid) pid = os::current_process_id();
1195
1196  if (os::win32::is_nt()) return _enumerate_modules_winnt  (pid, func, param);
1197  else                    return _enumerate_modules_windows(pid, func, param);
1198}
1199
1200struct _modinfo {
1201   address addr;
1202   char*   full_path;   // point to a char buffer
1203   int     buflen;      // size of the buffer
1204   address base_addr;
1205};
1206
1207static int _locate_module_by_addr(int pid, char * mod_fname, address base_addr,
1208                                  unsigned size, void * param) {
1209   struct _modinfo *pmod = (struct _modinfo *)param;
1210   if (!pmod) return -1;
1211
1212   if (base_addr     <= pmod->addr &&
1213       base_addr+size > pmod->addr) {
1214     // if a buffer is provided, copy path name to the buffer
1215     if (pmod->full_path) {
1216       jio_snprintf(pmod->full_path, pmod->buflen, "%s", mod_fname);
1217     }
1218     pmod->base_addr = base_addr;
1219     return 1;
1220   }
1221   return 0;
1222}
1223
1224bool os::dll_address_to_library_name(address addr, char* buf,
1225                                     int buflen, int* offset) {
1226// NOTE: the reason we don't use SymGetModuleInfo() is it doesn't always
1227//       return the full path to the DLL file, sometimes it returns path
1228//       to the corresponding PDB file (debug info); sometimes it only
1229//       returns partial path, which makes life painful.
1230
1231   struct _modinfo mi;
1232   mi.addr      = addr;
1233   mi.full_path = buf;
1234   mi.buflen    = buflen;
1235   int pid = os::current_process_id();
1236   if (enumerate_modules(pid, _locate_module_by_addr, (void *)&mi)) {
1237      // buf already contains path name
1238      if (offset) *offset = addr - mi.base_addr;
1239      return true;
1240   } else {
1241      if (buf) buf[0] = '\0';
1242      if (offset) *offset = -1;
1243      return false;
1244   }
1245}
1246
1247bool os::dll_address_to_function_name(address addr, char *buf,
1248                                      int buflen, int *offset) {
1249  // Unimplemented on Windows - in order to use SymGetSymFromAddr(),
1250  // we need to initialize imagehlp/dbghelp, then load symbol table
1251  // for every module. That's too much work to do after a fatal error.
1252  // For an example on how to implement this function, see 1.4.2.
1253  if (offset)  *offset  = -1;
1254  if (buf) buf[0] = '\0';
1255  return false;
1256}
1257
1258// save the start and end address of jvm.dll into param[0] and param[1]
1259static int _locate_jvm_dll(int pid, char* mod_fname, address base_addr,
1260                    unsigned size, void * param) {
1261   if (!param) return -1;
1262
1263   if (base_addr     <= (address)_locate_jvm_dll &&
1264       base_addr+size > (address)_locate_jvm_dll) {
1265         ((address*)param)[0] = base_addr;
1266         ((address*)param)[1] = base_addr + size;
1267         return 1;
1268   }
1269   return 0;
1270}
1271
1272address vm_lib_location[2];    // start and end address of jvm.dll
1273
1274// check if addr is inside jvm.dll
1275bool os::address_is_in_vm(address addr) {
1276  if (!vm_lib_location[0] || !vm_lib_location[1]) {
1277    int pid = os::current_process_id();
1278    if (!enumerate_modules(pid, _locate_jvm_dll, (void *)vm_lib_location)) {
1279      assert(false, "Can't find jvm module.");
1280      return false;
1281    }
1282  }
1283
1284  return (vm_lib_location[0] <= addr) && (addr < vm_lib_location[1]);
1285}
1286
1287// print module info; param is outputStream*
1288static int _print_module(int pid, char* fname, address base,
1289                         unsigned size, void* param) {
1290   if (!param) return -1;
1291
1292   outputStream* st = (outputStream*)param;
1293
1294   address end_addr = base + size;
1295   st->print(PTR_FORMAT " - " PTR_FORMAT " \t%s\n", base, end_addr, fname);
1296   return 0;
1297}
1298
1299// Loads .dll/.so and
1300// in case of error it checks if .dll/.so was built for the
1301// same architecture as Hotspot is running on
1302void * os::dll_load(const char *name, char *ebuf, int ebuflen)
1303{
1304  void * result = LoadLibrary(name);
1305  if (result != NULL)
1306  {
1307    return result;
1308  }
1309
1310  long errcode = GetLastError();
1311  if (errcode == ERROR_MOD_NOT_FOUND) {
1312    strncpy(ebuf, "Can't find dependent libraries", ebuflen-1);
1313    ebuf[ebuflen-1]='\0';
1314    return NULL;
1315  }
1316
1317  // Parsing dll below
1318  // If we can read dll-info and find that dll was built
1319  // for an architecture other than Hotspot is running in
1320  // - then print to buffer "DLL was built for a different architecture"
1321  // else call getLastErrorString to obtain system error message
1322
1323  // Read system error message into ebuf
1324  // It may or may not be overwritten below (in the for loop and just above)
1325  getLastErrorString(ebuf, (size_t) ebuflen);
1326  ebuf[ebuflen-1]='\0';
1327  int file_descriptor=::open(name, O_RDONLY | O_BINARY, 0);
1328  if (file_descriptor<0)
1329  {
1330    return NULL;
1331  }
1332
1333  uint32_t signature_offset;
1334  uint16_t lib_arch=0;
1335  bool failed_to_get_lib_arch=
1336  (
1337    //Go to position 3c in the dll
1338    (os::seek_to_file_offset(file_descriptor,IMAGE_FILE_PTR_TO_SIGNATURE)<0)
1339    ||
1340    // Read loacation of signature
1341    (sizeof(signature_offset)!=
1342      (os::read(file_descriptor, (void*)&signature_offset,sizeof(signature_offset))))
1343    ||
1344    //Go to COFF File Header in dll
1345    //that is located after"signature" (4 bytes long)
1346    (os::seek_to_file_offset(file_descriptor,
1347      signature_offset+IMAGE_FILE_SIGNATURE_LENGTH)<0)
1348    ||
1349    //Read field that contains code of architecture
1350    // that dll was build for
1351    (sizeof(lib_arch)!=
1352      (os::read(file_descriptor, (void*)&lib_arch,sizeof(lib_arch))))
1353  );
1354
1355  ::close(file_descriptor);
1356  if (failed_to_get_lib_arch)
1357  {
1358    // file i/o error - report getLastErrorString(...) msg
1359    return NULL;
1360  }
1361
1362  typedef struct
1363  {
1364    uint16_t arch_code;
1365    char* arch_name;
1366  } arch_t;
1367
1368  static const arch_t arch_array[]={
1369    {IMAGE_FILE_MACHINE_I386,      (char*)"IA 32"},
1370    {IMAGE_FILE_MACHINE_AMD64,     (char*)"AMD 64"},
1371    {IMAGE_FILE_MACHINE_IA64,      (char*)"IA 64"}
1372  };
1373  #if   (defined _M_IA64)
1374    static const uint16_t running_arch=IMAGE_FILE_MACHINE_IA64;
1375  #elif (defined _M_AMD64)
1376    static const uint16_t running_arch=IMAGE_FILE_MACHINE_AMD64;
1377  #elif (defined _M_IX86)
1378    static const uint16_t running_arch=IMAGE_FILE_MACHINE_I386;
1379  #else
1380    #error Method os::dll_load requires that one of following \
1381           is defined :_M_IA64,_M_AMD64 or _M_IX86
1382  #endif
1383
1384
1385  // Obtain a string for printf operation
1386  // lib_arch_str shall contain string what platform this .dll was built for
1387  // running_arch_str shall string contain what platform Hotspot was built for
1388  char *running_arch_str=NULL,*lib_arch_str=NULL;
1389  for (unsigned int i=0;i<ARRAY_SIZE(arch_array);i++)
1390  {
1391    if (lib_arch==arch_array[i].arch_code)
1392      lib_arch_str=arch_array[i].arch_name;
1393    if (running_arch==arch_array[i].arch_code)
1394      running_arch_str=arch_array[i].arch_name;
1395  }
1396
1397  assert(running_arch_str,
1398    "Didn't find runing architecture code in arch_array");
1399
1400  // If the architure is right
1401  // but some other error took place - report getLastErrorString(...) msg
1402  if (lib_arch == running_arch)
1403  {
1404    return NULL;
1405  }
1406
1407  if (lib_arch_str!=NULL)
1408  {
1409    ::_snprintf(ebuf, ebuflen-1,
1410      "Can't load %s-bit .dll on a %s-bit platform",
1411      lib_arch_str,running_arch_str);
1412  }
1413  else
1414  {
1415    // don't know what architecture this dll was build for
1416    ::_snprintf(ebuf, ebuflen-1,
1417      "Can't load this .dll (machine code=0x%x) on a %s-bit platform",
1418      lib_arch,running_arch_str);
1419  }
1420
1421  return NULL;
1422}
1423
1424
1425void os::print_dll_info(outputStream *st) {
1426   int pid = os::current_process_id();
1427   st->print_cr("Dynamic libraries:");
1428   enumerate_modules(pid, _print_module, (void *)st);
1429}
1430
1431void os::print_os_info(outputStream* st) {
1432   st->print("OS:");
1433
1434   OSVERSIONINFOEX osvi;
1435   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
1436   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1437
1438   if (!GetVersionEx((OSVERSIONINFO *)&osvi)) {
1439      st->print_cr("N/A");
1440      return;
1441   }
1442
1443   int os_vers = osvi.dwMajorVersion * 1000 + osvi.dwMinorVersion;
1444
1445   if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1446     switch (os_vers) {
1447       case 3051: st->print(" Windows NT 3.51"); break;
1448       case 4000: st->print(" Windows NT 4.0"); break;
1449       case 5000: st->print(" Windows 2000"); break;
1450       case 5001: st->print(" Windows XP"); break;
1451       case 5002: st->print(" Windows Server 2003 family"); break;
1452       case 6000: st->print(" Windows Vista"); break;
1453       default: // future windows, print out its major and minor versions
1454                st->print(" Windows NT %d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion);
1455     }
1456   } else {
1457     switch (os_vers) {
1458       case 4000: st->print(" Windows 95"); break;
1459       case 4010: st->print(" Windows 98"); break;
1460       case 4090: st->print(" Windows Me"); break;
1461       default: // future windows, print out its major and minor versions
1462                st->print(" Windows %d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion);
1463     }
1464   }
1465
1466   st->print(" Build %d", osvi.dwBuildNumber);
1467   st->print(" %s", osvi.szCSDVersion);           // service pack
1468   st->cr();
1469}
1470
1471void os::print_memory_info(outputStream* st) {
1472  st->print("Memory:");
1473  st->print(" %dk page", os::vm_page_size()>>10);
1474
1475  // FIXME: GlobalMemoryStatus() may return incorrect value if total memory
1476  // is larger than 4GB
1477  MEMORYSTATUS ms;
1478  GlobalMemoryStatus(&ms);
1479
1480  st->print(", physical %uk", os::physical_memory() >> 10);
1481  st->print("(%uk free)", os::available_memory() >> 10);
1482
1483  st->print(", swap %uk", ms.dwTotalPageFile >> 10);
1484  st->print("(%uk free)", ms.dwAvailPageFile >> 10);
1485  st->cr();
1486}
1487
1488void os::print_siginfo(outputStream *st, void *siginfo) {
1489  EXCEPTION_RECORD* er = (EXCEPTION_RECORD*)siginfo;
1490  st->print("siginfo:");
1491  st->print(" ExceptionCode=0x%x", er->ExceptionCode);
1492
1493  if (er->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
1494      er->NumberParameters >= 2) {
1495      switch (er->ExceptionInformation[0]) {
1496      case 0: st->print(", reading address"); break;
1497      case 1: st->print(", writing address"); break;
1498      default: st->print(", ExceptionInformation=" INTPTR_FORMAT,
1499                            er->ExceptionInformation[0]);
1500      }
1501      st->print(" " INTPTR_FORMAT, er->ExceptionInformation[1]);
1502  } else if (er->ExceptionCode == EXCEPTION_IN_PAGE_ERROR &&
1503             er->NumberParameters >= 2 && UseSharedSpaces) {
1504    FileMapInfo* mapinfo = FileMapInfo::current_info();
1505    if (mapinfo->is_in_shared_space((void*)er->ExceptionInformation[1])) {
1506      st->print("\n\nError accessing class data sharing archive."       \
1507                " Mapped file inaccessible during execution, "          \
1508                " possible disk/network problem.");
1509    }
1510  } else {
1511    int num = er->NumberParameters;
1512    if (num > 0) {
1513      st->print(", ExceptionInformation=");
1514      for (int i = 0; i < num; i++) {
1515        st->print(INTPTR_FORMAT " ", er->ExceptionInformation[i]);
1516      }
1517    }
1518  }
1519  st->cr();
1520}
1521
1522void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
1523  // do nothing
1524}
1525
1526static char saved_jvm_path[MAX_PATH] = {0};
1527
1528// Find the full path to the current module, jvm.dll or jvm_g.dll
1529void os::jvm_path(char *buf, jint buflen) {
1530  // Error checking.
1531  if (buflen < MAX_PATH) {
1532    assert(false, "must use a large-enough buffer");
1533    buf[0] = '\0';
1534    return;
1535  }
1536  // Lazy resolve the path to current module.
1537  if (saved_jvm_path[0] != 0) {
1538    strcpy(buf, saved_jvm_path);
1539    return;
1540  }
1541
1542  GetModuleFileName(vm_lib_handle, buf, buflen);
1543  strcpy(saved_jvm_path, buf);
1544}
1545
1546
1547void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
1548#ifndef _WIN64
1549  st->print("_");
1550#endif
1551}
1552
1553
1554void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
1555#ifndef _WIN64
1556  st->print("@%d", args_size  * sizeof(int));
1557#endif
1558}
1559
1560// sun.misc.Signal
1561// NOTE that this is a workaround for an apparent kernel bug where if
1562// a signal handler for SIGBREAK is installed then that signal handler
1563// takes priority over the console control handler for CTRL_CLOSE_EVENT.
1564// See bug 4416763.
1565static void (*sigbreakHandler)(int) = NULL;
1566
1567static void UserHandler(int sig, void *siginfo, void *context) {
1568  os::signal_notify(sig);
1569  // We need to reinstate the signal handler each time...
1570  os::signal(sig, (void*)UserHandler);
1571}
1572
1573void* os::user_handler() {
1574  return (void*) UserHandler;
1575}
1576
1577void* os::signal(int signal_number, void* handler) {
1578  if ((signal_number == SIGBREAK) && (!ReduceSignalUsage)) {
1579    void (*oldHandler)(int) = sigbreakHandler;
1580    sigbreakHandler = (void (*)(int)) handler;
1581    return (void*) oldHandler;
1582  } else {
1583    return (void*)::signal(signal_number, (void (*)(int))handler);
1584  }
1585}
1586
1587void os::signal_raise(int signal_number) {
1588  raise(signal_number);
1589}
1590
1591// The Win32 C runtime library maps all console control events other than ^C
1592// into SIGBREAK, which makes it impossible to distinguish ^BREAK from close,
1593// logoff, and shutdown events.  We therefore install our own console handler
1594// that raises SIGTERM for the latter cases.
1595//
1596static BOOL WINAPI consoleHandler(DWORD event) {
1597  switch(event) {
1598    case CTRL_C_EVENT:
1599      if (is_error_reported()) {
1600        // Ctrl-C is pressed during error reporting, likely because the error
1601        // handler fails to abort. Let VM die immediately.
1602        os::die();
1603      }
1604
1605      os::signal_raise(SIGINT);
1606      return TRUE;
1607      break;
1608    case CTRL_BREAK_EVENT:
1609      if (sigbreakHandler != NULL) {
1610        (*sigbreakHandler)(SIGBREAK);
1611      }
1612      return TRUE;
1613      break;
1614    case CTRL_CLOSE_EVENT:
1615    case CTRL_LOGOFF_EVENT:
1616    case CTRL_SHUTDOWN_EVENT:
1617      os::signal_raise(SIGTERM);
1618      return TRUE;
1619      break;
1620    default:
1621      break;
1622  }
1623  return FALSE;
1624}
1625
1626/*
1627 * The following code is moved from os.cpp for making this
1628 * code platform specific, which it is by its very nature.
1629 */
1630
1631// Return maximum OS signal used + 1 for internal use only
1632// Used as exit signal for signal_thread
1633int os::sigexitnum_pd(){
1634  return NSIG;
1635}
1636
1637// a counter for each possible signal value, including signal_thread exit signal
1638static volatile jint pending_signals[NSIG+1] = { 0 };
1639static HANDLE sig_sem;
1640
1641void os::signal_init_pd() {
1642  // Initialize signal structures
1643  memset((void*)pending_signals, 0, sizeof(pending_signals));
1644
1645  sig_sem = ::CreateSemaphore(NULL, 0, NSIG+1, NULL);
1646
1647  // Programs embedding the VM do not want it to attempt to receive
1648  // events like CTRL_LOGOFF_EVENT, which are used to implement the
1649  // shutdown hooks mechanism introduced in 1.3.  For example, when
1650  // the VM is run as part of a Windows NT service (i.e., a servlet
1651  // engine in a web server), the correct behavior is for any console
1652  // control handler to return FALSE, not TRUE, because the OS's
1653  // "final" handler for such events allows the process to continue if
1654  // it is a service (while terminating it if it is not a service).
1655  // To make this behavior uniform and the mechanism simpler, we
1656  // completely disable the VM's usage of these console events if -Xrs
1657  // (=ReduceSignalUsage) is specified.  This means, for example, that
1658  // the CTRL-BREAK thread dump mechanism is also disabled in this
1659  // case.  See bugs 4323062, 4345157, and related bugs.
1660
1661  if (!ReduceSignalUsage) {
1662    // Add a CTRL-C handler
1663    SetConsoleCtrlHandler(consoleHandler, TRUE);
1664  }
1665}
1666
1667void os::signal_notify(int signal_number) {
1668  BOOL ret;
1669
1670  Atomic::inc(&pending_signals[signal_number]);
1671  ret = ::ReleaseSemaphore(sig_sem, 1, NULL);
1672  assert(ret != 0, "ReleaseSemaphore() failed");
1673}
1674
1675static int check_pending_signals(bool wait_for_signal) {
1676  DWORD ret;
1677  while (true) {
1678    for (int i = 0; i < NSIG + 1; i++) {
1679      jint n = pending_signals[i];
1680      if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
1681        return i;
1682      }
1683    }
1684    if (!wait_for_signal) {
1685      return -1;
1686    }
1687
1688    JavaThread *thread = JavaThread::current();
1689
1690    ThreadBlockInVM tbivm(thread);
1691
1692    bool threadIsSuspended;
1693    do {
1694      thread->set_suspend_equivalent();
1695      // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
1696      ret = ::WaitForSingleObject(sig_sem, INFINITE);
1697      assert(ret == WAIT_OBJECT_0, "WaitForSingleObject() failed");
1698
1699      // were we externally suspended while we were waiting?
1700      threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
1701      if (threadIsSuspended) {
1702        //
1703        // The semaphore has been incremented, but while we were waiting
1704        // another thread suspended us. We don't want to continue running
1705        // while suspended because that would surprise the thread that
1706        // suspended us.
1707        //
1708        ret = ::ReleaseSemaphore(sig_sem, 1, NULL);
1709        assert(ret != 0, "ReleaseSemaphore() failed");
1710
1711        thread->java_suspend_self();
1712      }
1713    } while (threadIsSuspended);
1714  }
1715}
1716
1717int os::signal_lookup() {
1718  return check_pending_signals(false);
1719}
1720
1721int os::signal_wait() {
1722  return check_pending_signals(true);
1723}
1724
1725// Implicit OS exception handling
1726
1727LONG Handle_Exception(struct _EXCEPTION_POINTERS* exceptionInfo, address handler) {
1728  JavaThread* thread = JavaThread::current();
1729  // Save pc in thread
1730#ifdef _M_IA64
1731  thread->set_saved_exception_pc((address)exceptionInfo->ContextRecord->StIIP);
1732  // Set pc to handler
1733  exceptionInfo->ContextRecord->StIIP = (DWORD64)handler;
1734#elif _M_AMD64
1735  thread->set_saved_exception_pc((address)exceptionInfo->ContextRecord->Rip);
1736  // Set pc to handler
1737  exceptionInfo->ContextRecord->Rip = (DWORD64)handler;
1738#else
1739  thread->set_saved_exception_pc((address)exceptionInfo->ContextRecord->Eip);
1740  // Set pc to handler
1741  exceptionInfo->ContextRecord->Eip = (LONG)handler;
1742#endif
1743
1744  // Continue the execution
1745  return EXCEPTION_CONTINUE_EXECUTION;
1746}
1747
1748
1749// Used for PostMortemDump
1750extern "C" void safepoints();
1751extern "C" void find(int x);
1752extern "C" void events();
1753
1754// According to Windows API documentation, an illegal instruction sequence should generate
1755// the 0xC000001C exception code. However, real world experience shows that occasionnaly
1756// the execution of an illegal instruction can generate the exception code 0xC000001E. This
1757// seems to be an undocumented feature of Win NT 4.0 (and probably other Windows systems).
1758
1759#define EXCEPTION_ILLEGAL_INSTRUCTION_2 0xC000001E
1760
1761// From "Execution Protection in the Windows Operating System" draft 0.35
1762// Once a system header becomes available, the "real" define should be
1763// included or copied here.
1764#define EXCEPTION_INFO_EXEC_VIOLATION 0x08
1765
1766#define def_excpt(val) #val, val
1767
1768struct siglabel {
1769  char *name;
1770  int   number;
1771};
1772
1773struct siglabel exceptlabels[] = {
1774    def_excpt(EXCEPTION_ACCESS_VIOLATION),
1775    def_excpt(EXCEPTION_DATATYPE_MISALIGNMENT),
1776    def_excpt(EXCEPTION_BREAKPOINT),
1777    def_excpt(EXCEPTION_SINGLE_STEP),
1778    def_excpt(EXCEPTION_ARRAY_BOUNDS_EXCEEDED),
1779    def_excpt(EXCEPTION_FLT_DENORMAL_OPERAND),
1780    def_excpt(EXCEPTION_FLT_DIVIDE_BY_ZERO),
1781    def_excpt(EXCEPTION_FLT_INEXACT_RESULT),
1782    def_excpt(EXCEPTION_FLT_INVALID_OPERATION),
1783    def_excpt(EXCEPTION_FLT_OVERFLOW),
1784    def_excpt(EXCEPTION_FLT_STACK_CHECK),
1785    def_excpt(EXCEPTION_FLT_UNDERFLOW),
1786    def_excpt(EXCEPTION_INT_DIVIDE_BY_ZERO),
1787    def_excpt(EXCEPTION_INT_OVERFLOW),
1788    def_excpt(EXCEPTION_PRIV_INSTRUCTION),
1789    def_excpt(EXCEPTION_IN_PAGE_ERROR),
1790    def_excpt(EXCEPTION_ILLEGAL_INSTRUCTION),
1791    def_excpt(EXCEPTION_ILLEGAL_INSTRUCTION_2),
1792    def_excpt(EXCEPTION_NONCONTINUABLE_EXCEPTION),
1793    def_excpt(EXCEPTION_STACK_OVERFLOW),
1794    def_excpt(EXCEPTION_INVALID_DISPOSITION),
1795    def_excpt(EXCEPTION_GUARD_PAGE),
1796    def_excpt(EXCEPTION_INVALID_HANDLE),
1797    NULL, 0
1798};
1799
1800const char* os::exception_name(int exception_code, char *buf, size_t size) {
1801  for (int i = 0; exceptlabels[i].name != NULL; i++) {
1802    if (exceptlabels[i].number == exception_code) {
1803       jio_snprintf(buf, size, "%s", exceptlabels[i].name);
1804       return buf;
1805    }
1806  }
1807
1808  return NULL;
1809}
1810
1811//-----------------------------------------------------------------------------
1812LONG Handle_IDiv_Exception(struct _EXCEPTION_POINTERS* exceptionInfo) {
1813  // handle exception caused by idiv; should only happen for -MinInt/-1
1814  // (division by zero is handled explicitly)
1815#ifdef _M_IA64
1816  assert(0, "Fix Handle_IDiv_Exception");
1817#elif _M_AMD64
1818  PCONTEXT ctx = exceptionInfo->ContextRecord;
1819  address pc = (address)ctx->Rip;
1820  NOT_PRODUCT(Events::log("idiv overflow exception at " INTPTR_FORMAT , pc));
1821  assert(pc[0] == 0xF7, "not an idiv opcode");
1822  assert((pc[1] & ~0x7) == 0xF8, "cannot handle non-register operands");
1823  assert(ctx->Rax == min_jint, "unexpected idiv exception");
1824  // set correct result values and continue after idiv instruction
1825  ctx->Rip = (DWORD)pc + 2;        // idiv reg, reg  is 2 bytes
1826  ctx->Rax = (DWORD)min_jint;      // result
1827  ctx->Rdx = (DWORD)0;             // remainder
1828  // Continue the execution
1829#else
1830  PCONTEXT ctx = exceptionInfo->ContextRecord;
1831  address pc = (address)ctx->Eip;
1832  NOT_PRODUCT(Events::log("idiv overflow exception at " INTPTR_FORMAT , pc));
1833  assert(pc[0] == 0xF7, "not an idiv opcode");
1834  assert((pc[1] & ~0x7) == 0xF8, "cannot handle non-register operands");
1835  assert(ctx->Eax == min_jint, "unexpected idiv exception");
1836  // set correct result values and continue after idiv instruction
1837  ctx->Eip = (DWORD)pc + 2;        // idiv reg, reg  is 2 bytes
1838  ctx->Eax = (DWORD)min_jint;      // result
1839  ctx->Edx = (DWORD)0;             // remainder
1840  // Continue the execution
1841#endif
1842  return EXCEPTION_CONTINUE_EXECUTION;
1843}
1844
1845#ifndef  _WIN64
1846//-----------------------------------------------------------------------------
1847LONG WINAPI Handle_FLT_Exception(struct _EXCEPTION_POINTERS* exceptionInfo) {
1848  // handle exception caused by native mothod modifying control word
1849  PCONTEXT ctx = exceptionInfo->ContextRecord;
1850  DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
1851
1852  switch (exception_code) {
1853    case EXCEPTION_FLT_DENORMAL_OPERAND:
1854    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
1855    case EXCEPTION_FLT_INEXACT_RESULT:
1856    case EXCEPTION_FLT_INVALID_OPERATION:
1857    case EXCEPTION_FLT_OVERFLOW:
1858    case EXCEPTION_FLT_STACK_CHECK:
1859    case EXCEPTION_FLT_UNDERFLOW:
1860      jint fp_control_word = (* (jint*) StubRoutines::addr_fpu_cntrl_wrd_std());
1861      if (fp_control_word != ctx->FloatSave.ControlWord) {
1862        // Restore FPCW and mask out FLT exceptions
1863        ctx->FloatSave.ControlWord = fp_control_word | 0xffffffc0;
1864        // Mask out pending FLT exceptions
1865        ctx->FloatSave.StatusWord &=  0xffffff00;
1866        return EXCEPTION_CONTINUE_EXECUTION;
1867      }
1868  }
1869  return EXCEPTION_CONTINUE_SEARCH;
1870}
1871#else //_WIN64
1872/*
1873  On Windows, the mxcsr control bits are non-volatile across calls
1874  See also CR 6192333
1875  If EXCEPTION_FLT_* happened after some native method modified
1876  mxcsr - it is not a jvm fault.
1877  However should we decide to restore of mxcsr after a faulty
1878  native method we can uncomment following code
1879      jint MxCsr = INITIAL_MXCSR;
1880        // we can't use StubRoutines::addr_mxcsr_std()
1881        // because in Win64 mxcsr is not saved there
1882      if (MxCsr != ctx->MxCsr) {
1883        ctx->MxCsr = MxCsr;
1884        return EXCEPTION_CONTINUE_EXECUTION;
1885      }
1886
1887*/
1888#endif //_WIN64
1889
1890
1891// Fatal error reporting is single threaded so we can make this a
1892// static and preallocated.  If it's more than MAX_PATH silently ignore
1893// it.
1894static char saved_error_file[MAX_PATH] = {0};
1895
1896void os::set_error_file(const char *logfile) {
1897  if (strlen(logfile) <= MAX_PATH) {
1898    strncpy(saved_error_file, logfile, MAX_PATH);
1899  }
1900}
1901
1902static inline void report_error(Thread* t, DWORD exception_code,
1903                                address addr, void* siginfo, void* context) {
1904  VMError err(t, exception_code, addr, siginfo, context);
1905  err.report_and_die();
1906
1907  // If UseOsErrorReporting, this will return here and save the error file
1908  // somewhere where we can find it in the minidump.
1909}
1910
1911//-----------------------------------------------------------------------------
1912LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
1913  if (InterceptOSException) return EXCEPTION_CONTINUE_SEARCH;
1914  DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
1915#ifdef _M_IA64
1916  address pc = (address) exceptionInfo->ContextRecord->StIIP;
1917#elif _M_AMD64
1918  address pc = (address) exceptionInfo->ContextRecord->Rip;
1919#else
1920  address pc = (address) exceptionInfo->ContextRecord->Eip;
1921#endif
1922  Thread* t = ThreadLocalStorage::get_thread_slow();          // slow & steady
1923
1924#ifndef _WIN64
1925  // Execution protection violation - win32 running on AMD64 only
1926  // Handled first to avoid misdiagnosis as a "normal" access violation;
1927  // This is safe to do because we have a new/unique ExceptionInformation
1928  // code for this condition.
1929  if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
1930    PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
1931    int exception_subcode = (int) exceptionRecord->ExceptionInformation[0];
1932    address addr = (address) exceptionRecord->ExceptionInformation[1];
1933
1934    if (exception_subcode == EXCEPTION_INFO_EXEC_VIOLATION) {
1935      int page_size = os::vm_page_size();
1936
1937      // Make sure the pc and the faulting address are sane.
1938      //
1939      // If an instruction spans a page boundary, and the page containing
1940      // the beginning of the instruction is executable but the following
1941      // page is not, the pc and the faulting address might be slightly
1942      // different - we still want to unguard the 2nd page in this case.
1943      //
1944      // 15 bytes seems to be a (very) safe value for max instruction size.
1945      bool pc_is_near_addr =
1946        (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
1947      bool instr_spans_page_boundary =
1948        (align_size_down((intptr_t) pc ^ (intptr_t) addr,
1949                         (intptr_t) page_size) > 0);
1950
1951      if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
1952        static volatile address last_addr =
1953          (address) os::non_memory_address_word();
1954
1955        // In conservative mode, don't unguard unless the address is in the VM
1956        if (UnguardOnExecutionViolation > 0 && addr != last_addr &&
1957            (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
1958
1959          // Unguard and retry
1960          address page_start =
1961            (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
1962          bool res = os::unguard_memory((char*) page_start, page_size);
1963
1964          if (PrintMiscellaneous && Verbose) {
1965            char buf[256];
1966            jio_snprintf(buf, sizeof(buf), "Execution protection violation "
1967                         "at " INTPTR_FORMAT
1968                         ", unguarding " INTPTR_FORMAT ": %s", addr,
1969                         page_start, (res ? "success" : strerror(errno)));
1970            tty->print_raw_cr(buf);
1971          }
1972
1973          // Set last_addr so if we fault again at the same address, we don't
1974          // end up in an endless loop.
1975          //
1976          // There are two potential complications here.  Two threads trapping
1977          // at the same address at the same time could cause one of the
1978          // threads to think it already unguarded, and abort the VM.  Likely
1979          // very rare.
1980          //
1981          // The other race involves two threads alternately trapping at
1982          // different addresses and failing to unguard the page, resulting in
1983          // an endless loop.  This condition is probably even more unlikely
1984          // than the first.
1985          //
1986          // Although both cases could be avoided by using locks or thread
1987          // local last_addr, these solutions are unnecessary complication:
1988          // this handler is a best-effort safety net, not a complete solution.
1989          // It is disabled by default and should only be used as a workaround
1990          // in case we missed any no-execute-unsafe VM code.
1991
1992          last_addr = addr;
1993
1994          return EXCEPTION_CONTINUE_EXECUTION;
1995        }
1996      }
1997
1998      // Last unguard failed or not unguarding
1999      tty->print_raw_cr("Execution protection violation");
2000      report_error(t, exception_code, addr, exceptionInfo->ExceptionRecord,
2001                   exceptionInfo->ContextRecord);
2002      return EXCEPTION_CONTINUE_SEARCH;
2003    }
2004  }
2005#endif // _WIN64
2006
2007  // Check to see if we caught the safepoint code in the
2008  // process of write protecting the memory serialization page.
2009  // It write enables the page immediately after protecting it
2010  // so just return.
2011  if ( exception_code == EXCEPTION_ACCESS_VIOLATION ) {
2012    JavaThread* thread = (JavaThread*) t;
2013    PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2014    address addr = (address) exceptionRecord->ExceptionInformation[1];
2015    if ( os::is_memory_serialize_page(thread, addr) ) {
2016      // Block current thread until the memory serialize page permission restored.
2017      os::block_on_serialize_page_trap();
2018      return EXCEPTION_CONTINUE_EXECUTION;
2019    }
2020  }
2021
2022
2023  if (t != NULL && t->is_Java_thread()) {
2024    JavaThread* thread = (JavaThread*) t;
2025    bool in_java = thread->thread_state() == _thread_in_Java;
2026
2027    // Handle potential stack overflows up front.
2028    if (exception_code == EXCEPTION_STACK_OVERFLOW) {
2029      if (os::uses_stack_guard_pages()) {
2030#ifdef _M_IA64
2031        //
2032        // If it's a legal stack address continue, Windows will map it in.
2033        //
2034        PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2035        address addr = (address) exceptionRecord->ExceptionInformation[1];
2036        if (addr > thread->stack_yellow_zone_base() && addr < thread->stack_base() )
2037          return EXCEPTION_CONTINUE_EXECUTION;
2038
2039        // The register save area is the same size as the memory stack
2040        // and starts at the page just above the start of the memory stack.
2041        // If we get a fault in this area, we've run out of register
2042        // stack.  If we are in java, try throwing a stack overflow exception.
2043        if (addr > thread->stack_base() &&
2044                      addr <= (thread->stack_base()+thread->stack_size()) ) {
2045          char buf[256];
2046          jio_snprintf(buf, sizeof(buf),
2047                       "Register stack overflow, addr:%p, stack_base:%p\n",
2048                       addr, thread->stack_base() );
2049          tty->print_raw_cr(buf);
2050          // If not in java code, return and hope for the best.
2051          return in_java ? Handle_Exception(exceptionInfo,
2052            SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW))
2053            :  EXCEPTION_CONTINUE_EXECUTION;
2054        }
2055#endif
2056        if (thread->stack_yellow_zone_enabled()) {
2057          // Yellow zone violation.  The o/s has unprotected the first yellow
2058          // zone page for us.  Note:  must call disable_stack_yellow_zone to
2059          // update the enabled status, even if the zone contains only one page.
2060          thread->disable_stack_yellow_zone();
2061          // If not in java code, return and hope for the best.
2062          return in_java ? Handle_Exception(exceptionInfo,
2063            SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW))
2064            :  EXCEPTION_CONTINUE_EXECUTION;
2065        } else {
2066          // Fatal red zone violation.
2067          thread->disable_stack_red_zone();
2068          tty->print_raw_cr("An unrecoverable stack overflow has occurred.");
2069          report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2070                       exceptionInfo->ContextRecord);
2071          return EXCEPTION_CONTINUE_SEARCH;
2072        }
2073      } else if (in_java) {
2074        // JVM-managed guard pages cannot be used on win95/98.  The o/s provides
2075        // a one-time-only guard page, which it has released to us.  The next
2076        // stack overflow on this thread will result in an ACCESS_VIOLATION.
2077        return Handle_Exception(exceptionInfo,
2078          SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
2079      } else {
2080        // Can only return and hope for the best.  Further stack growth will
2081        // result in an ACCESS_VIOLATION.
2082        return EXCEPTION_CONTINUE_EXECUTION;
2083      }
2084    } else if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2085      // Either stack overflow or null pointer exception.
2086      if (in_java) {
2087        PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2088        address addr = (address) exceptionRecord->ExceptionInformation[1];
2089        address stack_end = thread->stack_base() - thread->stack_size();
2090        if (addr < stack_end && addr >= stack_end - os::vm_page_size()) {
2091          // Stack overflow.
2092          assert(!os::uses_stack_guard_pages(),
2093            "should be caught by red zone code above.");
2094          return Handle_Exception(exceptionInfo,
2095            SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
2096        }
2097        //
2098        // Check for safepoint polling and implicit null
2099        // We only expect null pointers in the stubs (vtable)
2100        // the rest are checked explicitly now.
2101        //
2102        CodeBlob* cb = CodeCache::find_blob(pc);
2103        if (cb != NULL) {
2104          if (os::is_poll_address(addr)) {
2105            address stub = SharedRuntime::get_poll_stub(pc);
2106            return Handle_Exception(exceptionInfo, stub);
2107          }
2108        }
2109        {
2110#ifdef _WIN64
2111          //
2112          // If it's a legal stack address map the entire region in
2113          //
2114          PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2115          address addr = (address) exceptionRecord->ExceptionInformation[1];
2116          if (addr > thread->stack_yellow_zone_base() && addr < thread->stack_base() ) {
2117                  addr = (address)((uintptr_t)addr &
2118                         (~((uintptr_t)os::vm_page_size() - (uintptr_t)1)));
2119                  os::commit_memory( (char *)addr, thread->stack_base() - addr );
2120                  return EXCEPTION_CONTINUE_EXECUTION;
2121          }
2122          else
2123#endif
2124          {
2125            // Null pointer exception.
2126#ifdef _M_IA64
2127            // We catch register stack overflows in compiled code by doing
2128            // an explicit compare and executing a st8(G0, G0) if the
2129            // BSP enters into our guard area.  We test for the overflow
2130            // condition and fall into the normal null pointer exception
2131            // code if BSP hasn't overflowed.
2132            if ( in_java ) {
2133              if(thread->register_stack_overflow()) {
2134                assert((address)exceptionInfo->ContextRecord->IntS3 ==
2135                                thread->register_stack_limit(),
2136                               "GR7 doesn't contain register_stack_limit");
2137                // Disable the yellow zone which sets the state that
2138                // we've got a stack overflow problem.
2139                if (thread->stack_yellow_zone_enabled()) {
2140                  thread->disable_stack_yellow_zone();
2141                }
2142                // Give us some room to process the exception
2143                thread->disable_register_stack_guard();
2144                // Update GR7 with the new limit so we can continue running
2145                // compiled code.
2146                exceptionInfo->ContextRecord->IntS3 =
2147                               (ULONGLONG)thread->register_stack_limit();
2148                return Handle_Exception(exceptionInfo,
2149                       SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
2150              } else {
2151                //
2152                // Check for implicit null
2153                // We only expect null pointers in the stubs (vtable)
2154                // the rest are checked explicitly now.
2155                //
2156                CodeBlob* cb = CodeCache::find_blob(pc);
2157                if (cb != NULL) {
2158                  if (VtableStubs::stub_containing(pc) != NULL) {
2159                    if (((uintptr_t)addr) < os::vm_page_size() ) {
2160                      // an access to the first page of VM--assume it is a null pointer
2161                      return Handle_Exception(exceptionInfo,
2162                        SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL));
2163                    }
2164                  }
2165                }
2166              }
2167            } // in_java
2168
2169            // IA64 doesn't use implicit null checking yet. So we shouldn't
2170            // get here.
2171            tty->print_raw_cr("Access violation, possible null pointer exception");
2172            report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2173                         exceptionInfo->ContextRecord);
2174            return EXCEPTION_CONTINUE_SEARCH;
2175#else /* !IA64 */
2176
2177            // Windows 98 reports faulting addresses incorrectly
2178            if (!MacroAssembler::needs_explicit_null_check((intptr_t)addr) ||
2179                !os::win32::is_nt()) {
2180              return Handle_Exception(exceptionInfo,
2181                  SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL));
2182            }
2183            report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2184                         exceptionInfo->ContextRecord);
2185            return EXCEPTION_CONTINUE_SEARCH;
2186#endif
2187          }
2188        }
2189      }
2190
2191#ifdef _WIN64
2192      // Special care for fast JNI field accessors.
2193      // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks
2194      // in and the heap gets shrunk before the field access.
2195      if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2196        address addr = JNI_FastGetField::find_slowcase_pc(pc);
2197        if (addr != (address)-1) {
2198          return Handle_Exception(exceptionInfo, addr);
2199        }
2200      }
2201#endif
2202
2203#ifdef _WIN64
2204      // Windows will sometimes generate an access violation
2205      // when we call malloc.  Since we use VectoredExceptions
2206      // on 64 bit platforms, we see this exception.  We must
2207      // pass this exception on so Windows can recover.
2208      // We check to see if the pc of the fault is in NTDLL.DLL
2209      // if so, we pass control on to Windows for handling.
2210      if (UseVectoredExceptions && _addr_in_ntdll(pc)) return EXCEPTION_CONTINUE_SEARCH;
2211#endif
2212
2213      // Stack overflow or null pointer exception in native code.
2214      report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2215                   exceptionInfo->ContextRecord);
2216      return EXCEPTION_CONTINUE_SEARCH;
2217    }
2218
2219    if (in_java) {
2220      switch (exception_code) {
2221      case EXCEPTION_INT_DIVIDE_BY_ZERO:
2222        return Handle_Exception(exceptionInfo, SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO));
2223
2224      case EXCEPTION_INT_OVERFLOW:
2225        return Handle_IDiv_Exception(exceptionInfo);
2226
2227      } // switch
2228    }
2229#ifndef _WIN64
2230    if ((thread->thread_state() == _thread_in_Java) ||
2231        (thread->thread_state() == _thread_in_native) )
2232    {
2233      LONG result=Handle_FLT_Exception(exceptionInfo);
2234      if (result==EXCEPTION_CONTINUE_EXECUTION) return result;
2235    }
2236#endif //_WIN64
2237  }
2238
2239  if (exception_code != EXCEPTION_BREAKPOINT) {
2240#ifndef _WIN64
2241    report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2242                 exceptionInfo->ContextRecord);
2243#else
2244    // Itanium Windows uses a VectoredExceptionHandler
2245    // Which means that C++ programatic exception handlers (try/except)
2246    // will get here.  Continue the search for the right except block if
2247    // the exception code is not a fatal code.
2248    switch ( exception_code ) {
2249      case EXCEPTION_ACCESS_VIOLATION:
2250      case EXCEPTION_STACK_OVERFLOW:
2251      case EXCEPTION_ILLEGAL_INSTRUCTION:
2252      case EXCEPTION_ILLEGAL_INSTRUCTION_2:
2253      case EXCEPTION_INT_OVERFLOW:
2254      case EXCEPTION_INT_DIVIDE_BY_ZERO:
2255      {  report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2256                       exceptionInfo->ContextRecord);
2257      }
2258        break;
2259      default:
2260        break;
2261    }
2262#endif
2263  }
2264  return EXCEPTION_CONTINUE_SEARCH;
2265}
2266
2267#ifndef _WIN64
2268// Special care for fast JNI accessors.
2269// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in and
2270// the heap gets shrunk before the field access.
2271// Need to install our own structured exception handler since native code may
2272// install its own.
2273LONG WINAPI fastJNIAccessorExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
2274  DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
2275  if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2276    address pc = (address) exceptionInfo->ContextRecord->Eip;
2277    address addr = JNI_FastGetField::find_slowcase_pc(pc);
2278    if (addr != (address)-1) {
2279      return Handle_Exception(exceptionInfo, addr);
2280    }
2281  }
2282  return EXCEPTION_CONTINUE_SEARCH;
2283}
2284
2285#define DEFINE_FAST_GETFIELD(Return,Fieldname,Result) \
2286Return JNICALL jni_fast_Get##Result##Field_wrapper(JNIEnv *env, jobject obj, jfieldID fieldID) { \
2287  __try { \
2288    return (*JNI_FastGetField::jni_fast_Get##Result##Field_fp)(env, obj, fieldID); \
2289  } __except(fastJNIAccessorExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) { \
2290  } \
2291  return 0; \
2292}
2293
2294DEFINE_FAST_GETFIELD(jboolean, bool,   Boolean)
2295DEFINE_FAST_GETFIELD(jbyte,    byte,   Byte)
2296DEFINE_FAST_GETFIELD(jchar,    char,   Char)
2297DEFINE_FAST_GETFIELD(jshort,   short,  Short)
2298DEFINE_FAST_GETFIELD(jint,     int,    Int)
2299DEFINE_FAST_GETFIELD(jlong,    long,   Long)
2300DEFINE_FAST_GETFIELD(jfloat,   float,  Float)
2301DEFINE_FAST_GETFIELD(jdouble,  double, Double)
2302
2303address os::win32::fast_jni_accessor_wrapper(BasicType type) {
2304  switch (type) {
2305    case T_BOOLEAN: return (address)jni_fast_GetBooleanField_wrapper;
2306    case T_BYTE:    return (address)jni_fast_GetByteField_wrapper;
2307    case T_CHAR:    return (address)jni_fast_GetCharField_wrapper;
2308    case T_SHORT:   return (address)jni_fast_GetShortField_wrapper;
2309    case T_INT:     return (address)jni_fast_GetIntField_wrapper;
2310    case T_LONG:    return (address)jni_fast_GetLongField_wrapper;
2311    case T_FLOAT:   return (address)jni_fast_GetFloatField_wrapper;
2312    case T_DOUBLE:  return (address)jni_fast_GetDoubleField_wrapper;
2313    default:        ShouldNotReachHere();
2314  }
2315  return (address)-1;
2316}
2317#endif
2318
2319// Virtual Memory
2320
2321int os::vm_page_size() { return os::win32::vm_page_size(); }
2322int os::vm_allocation_granularity() {
2323  return os::win32::vm_allocation_granularity();
2324}
2325
2326// Windows large page support is available on Windows 2003. In order to use
2327// large page memory, the administrator must first assign additional privilege
2328// to the user:
2329//   + select Control Panel -> Administrative Tools -> Local Security Policy
2330//   + select Local Policies -> User Rights Assignment
2331//   + double click "Lock pages in memory", add users and/or groups
2332//   + reboot
2333// Note the above steps are needed for administrator as well, as administrators
2334// by default do not have the privilege to lock pages in memory.
2335//
2336// Note about Windows 2003: although the API supports committing large page
2337// memory on a page-by-page basis and VirtualAlloc() returns success under this
2338// scenario, I found through experiment it only uses large page if the entire
2339// memory region is reserved and committed in a single VirtualAlloc() call.
2340// This makes Windows large page support more or less like Solaris ISM, in
2341// that the entire heap must be committed upfront. This probably will change
2342// in the future, if so the code below needs to be revisited.
2343
2344#ifndef MEM_LARGE_PAGES
2345#define MEM_LARGE_PAGES 0x20000000
2346#endif
2347
2348// GetLargePageMinimum is only available on Windows 2003. The other functions
2349// are available on NT but not on Windows 98/Me. We have to resolve them at
2350// runtime.
2351typedef SIZE_T (WINAPI *GetLargePageMinimum_func_type) (void);
2352typedef BOOL (WINAPI *AdjustTokenPrivileges_func_type)
2353             (HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
2354typedef BOOL (WINAPI *OpenProcessToken_func_type) (HANDLE, DWORD, PHANDLE);
2355typedef BOOL (WINAPI *LookupPrivilegeValue_func_type) (LPCTSTR, LPCTSTR, PLUID);
2356
2357static GetLargePageMinimum_func_type   _GetLargePageMinimum;
2358static AdjustTokenPrivileges_func_type _AdjustTokenPrivileges;
2359static OpenProcessToken_func_type      _OpenProcessToken;
2360static LookupPrivilegeValue_func_type  _LookupPrivilegeValue;
2361
2362static HINSTANCE _kernel32;
2363static HINSTANCE _advapi32;
2364static HANDLE    _hProcess;
2365static HANDLE    _hToken;
2366
2367static size_t _large_page_size = 0;
2368
2369static bool resolve_functions_for_large_page_init() {
2370  _kernel32 = LoadLibrary("kernel32.dll");
2371  if (_kernel32 == NULL) return false;
2372
2373  _GetLargePageMinimum   = CAST_TO_FN_PTR(GetLargePageMinimum_func_type,
2374                            GetProcAddress(_kernel32, "GetLargePageMinimum"));
2375  if (_GetLargePageMinimum == NULL) return false;
2376
2377  _advapi32 = LoadLibrary("advapi32.dll");
2378  if (_advapi32 == NULL) return false;
2379
2380  _AdjustTokenPrivileges = CAST_TO_FN_PTR(AdjustTokenPrivileges_func_type,
2381                            GetProcAddress(_advapi32, "AdjustTokenPrivileges"));
2382  _OpenProcessToken      = CAST_TO_FN_PTR(OpenProcessToken_func_type,
2383                            GetProcAddress(_advapi32, "OpenProcessToken"));
2384  _LookupPrivilegeValue  = CAST_TO_FN_PTR(LookupPrivilegeValue_func_type,
2385                            GetProcAddress(_advapi32, "LookupPrivilegeValueA"));
2386  return _AdjustTokenPrivileges != NULL &&
2387         _OpenProcessToken      != NULL &&
2388         _LookupPrivilegeValue  != NULL;
2389}
2390
2391static bool request_lock_memory_privilege() {
2392  _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
2393                                os::current_process_id());
2394
2395  LUID luid;
2396  if (_hProcess != NULL &&
2397      _OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
2398      _LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
2399
2400    TOKEN_PRIVILEGES tp;
2401    tp.PrivilegeCount = 1;
2402    tp.Privileges[0].Luid = luid;
2403    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2404
2405    // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
2406    // privilege. Check GetLastError() too. See MSDN document.
2407    if (_AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
2408        (GetLastError() == ERROR_SUCCESS)) {
2409      return true;
2410    }
2411  }
2412
2413  return false;
2414}
2415
2416static void cleanup_after_large_page_init() {
2417  _GetLargePageMinimum = NULL;
2418  _AdjustTokenPrivileges = NULL;
2419  _OpenProcessToken = NULL;
2420  _LookupPrivilegeValue = NULL;
2421  if (_kernel32) FreeLibrary(_kernel32);
2422  _kernel32 = NULL;
2423  if (_advapi32) FreeLibrary(_advapi32);
2424  _advapi32 = NULL;
2425  if (_hProcess) CloseHandle(_hProcess);
2426  _hProcess = NULL;
2427  if (_hToken) CloseHandle(_hToken);
2428  _hToken = NULL;
2429}
2430
2431bool os::large_page_init() {
2432  if (!UseLargePages) return false;
2433
2434  // print a warning if any large page related flag is specified on command line
2435  bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages) ||
2436                         !FLAG_IS_DEFAULT(LargePageSizeInBytes);
2437  bool success = false;
2438
2439# define WARN(msg) if (warn_on_failure) { warning(msg); }
2440  if (resolve_functions_for_large_page_init()) {
2441    if (request_lock_memory_privilege()) {
2442      size_t s = _GetLargePageMinimum();
2443      if (s) {
2444#if defined(IA32) || defined(AMD64)
2445        if (s > 4*M || LargePageSizeInBytes > 4*M) {
2446          WARN("JVM cannot use large pages bigger than 4mb.");
2447        } else {
2448#endif
2449          if (LargePageSizeInBytes && LargePageSizeInBytes % s == 0) {
2450            _large_page_size = LargePageSizeInBytes;
2451          } else {
2452            _large_page_size = s;
2453          }
2454          success = true;
2455#if defined(IA32) || defined(AMD64)
2456        }
2457#endif
2458      } else {
2459        WARN("Large page is not supported by the processor.");
2460      }
2461    } else {
2462      WARN("JVM cannot use large page memory because it does not have enough privilege to lock pages in memory.");
2463    }
2464  } else {
2465    WARN("Large page is not supported by the operating system.");
2466  }
2467#undef WARN
2468
2469  const size_t default_page_size = (size_t) vm_page_size();
2470  if (success && _large_page_size > default_page_size) {
2471    _page_sizes[0] = _large_page_size;
2472    _page_sizes[1] = default_page_size;
2473    _page_sizes[2] = 0;
2474  }
2475
2476  cleanup_after_large_page_init();
2477  return success;
2478}
2479
2480// On win32, one cannot release just a part of reserved memory, it's an
2481// all or nothing deal.  When we split a reservation, we must break the
2482// reservation into two reservations.
2483void os::split_reserved_memory(char *base, size_t size, size_t split,
2484                              bool realloc) {
2485  if (size > 0) {
2486    release_memory(base, size);
2487    if (realloc) {
2488      reserve_memory(split, base);
2489    }
2490    if (size != split) {
2491      reserve_memory(size - split, base + split);
2492    }
2493  }
2494}
2495
2496char* os::reserve_memory(size_t bytes, char* addr, size_t alignment_hint) {
2497  assert((size_t)addr % os::vm_allocation_granularity() == 0,
2498         "reserve alignment");
2499  assert(bytes % os::vm_allocation_granularity() == 0, "reserve block size");
2500  char* res = (char*)VirtualAlloc(addr, bytes, MEM_RESERVE,
2501                                  PAGE_EXECUTE_READWRITE);
2502  assert(res == NULL || addr == NULL || addr == res,
2503         "Unexpected address from reserve.");
2504  return res;
2505}
2506
2507// Reserve memory at an arbitrary address, only if that area is
2508// available (and not reserved for something else).
2509char* os::attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
2510  // Windows os::reserve_memory() fails of the requested address range is
2511  // not avilable.
2512  return reserve_memory(bytes, requested_addr);
2513}
2514
2515size_t os::large_page_size() {
2516  return _large_page_size;
2517}
2518
2519bool os::can_commit_large_page_memory() {
2520  // Windows only uses large page memory when the entire region is reserved
2521  // and committed in a single VirtualAlloc() call. This may change in the
2522  // future, but with Windows 2003 it's not possible to commit on demand.
2523  return false;
2524}
2525
2526char* os::reserve_memory_special(size_t bytes) {
2527  DWORD flag = MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES;
2528  char * res = (char *)VirtualAlloc(NULL, bytes, flag, PAGE_READWRITE);
2529  return res;
2530}
2531
2532bool os::release_memory_special(char* base, size_t bytes) {
2533  return release_memory(base, bytes);
2534}
2535
2536void os::print_statistics() {
2537}
2538
2539bool os::commit_memory(char* addr, size_t bytes) {
2540  if (bytes == 0) {
2541    // Don't bother the OS with noops.
2542    return true;
2543  }
2544  assert((size_t) addr % os::vm_page_size() == 0, "commit on page boundaries");
2545  assert(bytes % os::vm_page_size() == 0, "commit in page-sized chunks");
2546  // Don't attempt to print anything if the OS call fails. We're
2547  // probably low on resources, so the print itself may cause crashes.
2548  return VirtualAlloc(addr, bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE) != NULL;
2549}
2550
2551bool os::commit_memory(char* addr, size_t size, size_t alignment_hint) {
2552  return commit_memory(addr, size);
2553}
2554
2555bool os::uncommit_memory(char* addr, size_t bytes) {
2556  if (bytes == 0) {
2557    // Don't bother the OS with noops.
2558    return true;
2559  }
2560  assert((size_t) addr % os::vm_page_size() == 0, "uncommit on page boundaries");
2561  assert(bytes % os::vm_page_size() == 0, "uncommit in page-sized chunks");
2562  return VirtualFree(addr, bytes, MEM_DECOMMIT) != 0;
2563}
2564
2565bool os::release_memory(char* addr, size_t bytes) {
2566  return VirtualFree(addr, 0, MEM_RELEASE) != 0;
2567}
2568
2569bool os::protect_memory(char* addr, size_t bytes) {
2570  DWORD old_status;
2571  return VirtualProtect(addr, bytes, PAGE_READONLY, &old_status) != 0;
2572}
2573
2574bool os::guard_memory(char* addr, size_t bytes) {
2575  DWORD old_status;
2576  return VirtualProtect(addr, bytes, PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_status) != 0;
2577}
2578
2579bool os::unguard_memory(char* addr, size_t bytes) {
2580  DWORD old_status;
2581  return VirtualProtect(addr, bytes, PAGE_EXECUTE_READWRITE, &old_status) != 0;
2582}
2583
2584void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) { }
2585void os::free_memory(char *addr, size_t bytes)         { }
2586void os::numa_make_global(char *addr, size_t bytes)    { }
2587void os::numa_make_local(char *addr, size_t bytes)     { }
2588bool os::numa_topology_changed()                       { return false; }
2589size_t os::numa_get_groups_num()                       { return 1; }
2590int os::numa_get_group_id()                            { return 0; }
2591size_t os::numa_get_leaf_groups(int *ids, size_t size) {
2592  if (size > 0) {
2593    ids[0] = 0;
2594    return 1;
2595  }
2596  return 0;
2597}
2598
2599bool os::get_page_info(char *start, page_info* info) {
2600  return false;
2601}
2602
2603char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {
2604  return end;
2605}
2606
2607char* os::non_memory_address_word() {
2608  // Must never look like an address returned by reserve_memory,
2609  // even in its subfields (as defined by the CPU immediate fields,
2610  // if the CPU splits constants across multiple instructions).
2611  return (char*)-1;
2612}
2613
2614#define MAX_ERROR_COUNT 100
2615#define SYS_THREAD_ERROR 0xffffffffUL
2616
2617void os::pd_start_thread(Thread* thread) {
2618  DWORD ret = ResumeThread(thread->osthread()->thread_handle());
2619  // Returns previous suspend state:
2620  // 0:  Thread was not suspended
2621  // 1:  Thread is running now
2622  // >1: Thread is still suspended.
2623  assert(ret != SYS_THREAD_ERROR, "StartThread failed"); // should propagate back
2624}
2625
2626size_t os::read(int fd, void *buf, unsigned int nBytes) {
2627  return ::read(fd, buf, nBytes);
2628}
2629
2630class HighResolutionInterval {
2631  // The default timer resolution seems to be 10 milliseconds.
2632  // (Where is this written down?)
2633  // If someone wants to sleep for only a fraction of the default,
2634  // then we set the timer resolution down to 1 millisecond for
2635  // the duration of their interval.
2636  // We carefully set the resolution back, since otherwise we
2637  // seem to incur an overhead (3%?) that we don't need.
2638  // CONSIDER: if ms is small, say 3, then we should run with a high resolution time.
2639  // Buf if ms is large, say 500, or 503, we should avoid the call to timeBeginPeriod().
2640  // Alternatively, we could compute the relative error (503/500 = .6%) and only use
2641  // timeBeginPeriod() if the relative error exceeded some threshold.
2642  // timeBeginPeriod() has been linked to problems with clock drift on win32 systems and
2643  // to decreased efficiency related to increased timer "tick" rates.  We want to minimize
2644  // (a) calls to timeBeginPeriod() and timeEndPeriod() and (b) time spent with high
2645  // resolution timers running.
2646private:
2647    jlong resolution;
2648public:
2649  HighResolutionInterval(jlong ms) {
2650    resolution = ms % 10L;
2651    if (resolution != 0) {
2652      MMRESULT result = timeBeginPeriod(1L);
2653    }
2654  }
2655  ~HighResolutionInterval() {
2656    if (resolution != 0) {
2657      MMRESULT result = timeEndPeriod(1L);
2658    }
2659    resolution = 0L;
2660  }
2661};
2662
2663int os::sleep(Thread* thread, jlong ms, bool interruptable) {
2664  jlong limit = (jlong) MAXDWORD;
2665
2666  while(ms > limit) {
2667    int res;
2668    if ((res = sleep(thread, limit, interruptable)) != OS_TIMEOUT)
2669      return res;
2670    ms -= limit;
2671  }
2672
2673  assert(thread == Thread::current(),  "thread consistency check");
2674  OSThread* osthread = thread->osthread();
2675  OSThreadWaitState osts(osthread, false /* not Object.wait() */);
2676  int result;
2677  if (interruptable) {
2678    assert(thread->is_Java_thread(), "must be java thread");
2679    JavaThread *jt = (JavaThread *) thread;
2680    ThreadBlockInVM tbivm(jt);
2681
2682    jt->set_suspend_equivalent();
2683    // cleared by handle_special_suspend_equivalent_condition() or
2684    // java_suspend_self() via check_and_wait_while_suspended()
2685
2686    HANDLE events[1];
2687    events[0] = osthread->interrupt_event();
2688    HighResolutionInterval *phri=NULL;
2689    if(!ForceTimeHighResolution)
2690      phri = new HighResolutionInterval( ms );
2691    if (WaitForMultipleObjects(1, events, FALSE, (DWORD)ms) == WAIT_TIMEOUT) {
2692      result = OS_TIMEOUT;
2693    } else {
2694      ResetEvent(osthread->interrupt_event());
2695      osthread->set_interrupted(false);
2696      result = OS_INTRPT;
2697    }
2698    delete phri; //if it is NULL, harmless
2699
2700    // were we externally suspended while we were waiting?
2701    jt->check_and_wait_while_suspended();
2702  } else {
2703    assert(!thread->is_Java_thread(), "must not be java thread");
2704    Sleep((long) ms);
2705    result = OS_TIMEOUT;
2706  }
2707  return result;
2708}
2709
2710// Sleep forever; naked call to OS-specific sleep; use with CAUTION
2711void os::infinite_sleep() {
2712  while (true) {    // sleep forever ...
2713    Sleep(100000);  // ... 100 seconds at a time
2714  }
2715}
2716
2717typedef BOOL (WINAPI * STTSignature)(void) ;
2718
2719os::YieldResult os::NakedYield() {
2720  // Use either SwitchToThread() or Sleep(0)
2721  // Consider passing back the return value from SwitchToThread().
2722  // We use GetProcAddress() as ancient Win9X versions of windows doen't support SwitchToThread.
2723  // In that case we revert to Sleep(0).
2724  static volatile STTSignature stt = (STTSignature) 1 ;
2725
2726  if (stt == ((STTSignature) 1)) {
2727    stt = (STTSignature) ::GetProcAddress (LoadLibrary ("Kernel32.dll"), "SwitchToThread") ;
2728    // It's OK if threads race during initialization as the operation above is idempotent.
2729  }
2730  if (stt != NULL) {
2731    return (*stt)() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
2732  } else {
2733    Sleep (0) ;
2734  }
2735  return os::YIELD_UNKNOWN ;
2736}
2737
2738void os::yield() {  os::NakedYield(); }
2739
2740void os::yield_all(int attempts) {
2741  // Yields to all threads, including threads with lower priorities
2742  Sleep(1);
2743}
2744
2745// Win32 only gives you access to seven real priorities at a time,
2746// so we compress Java's ten down to seven.  It would be better
2747// if we dynamically adjusted relative priorities.
2748
2749int os::java_to_os_priority[MaxPriority + 1] = {
2750  THREAD_PRIORITY_IDLE,                         // 0  Entry should never be used
2751  THREAD_PRIORITY_LOWEST,                       // 1  MinPriority
2752  THREAD_PRIORITY_LOWEST,                       // 2
2753  THREAD_PRIORITY_BELOW_NORMAL,                 // 3
2754  THREAD_PRIORITY_BELOW_NORMAL,                 // 4
2755  THREAD_PRIORITY_NORMAL,                       // 5  NormPriority
2756  THREAD_PRIORITY_NORMAL,                       // 6
2757  THREAD_PRIORITY_ABOVE_NORMAL,                 // 7
2758  THREAD_PRIORITY_ABOVE_NORMAL,                 // 8
2759  THREAD_PRIORITY_HIGHEST,                      // 9  NearMaxPriority
2760  THREAD_PRIORITY_HIGHEST                       // 10 MaxPriority
2761};
2762
2763int prio_policy1[MaxPriority + 1] = {
2764  THREAD_PRIORITY_IDLE,                         // 0  Entry should never be used
2765  THREAD_PRIORITY_LOWEST,                       // 1  MinPriority
2766  THREAD_PRIORITY_LOWEST,                       // 2
2767  THREAD_PRIORITY_BELOW_NORMAL,                 // 3
2768  THREAD_PRIORITY_BELOW_NORMAL,                 // 4
2769  THREAD_PRIORITY_NORMAL,                       // 5  NormPriority
2770  THREAD_PRIORITY_ABOVE_NORMAL,                 // 6
2771  THREAD_PRIORITY_ABOVE_NORMAL,                 // 7
2772  THREAD_PRIORITY_HIGHEST,                      // 8
2773  THREAD_PRIORITY_HIGHEST,                      // 9  NearMaxPriority
2774  THREAD_PRIORITY_TIME_CRITICAL                 // 10 MaxPriority
2775};
2776
2777static int prio_init() {
2778  // If ThreadPriorityPolicy is 1, switch tables
2779  if (ThreadPriorityPolicy == 1) {
2780    int i;
2781    for (i = 0; i < MaxPriority + 1; i++) {
2782      os::java_to_os_priority[i] = prio_policy1[i];
2783    }
2784  }
2785  return 0;
2786}
2787
2788OSReturn os::set_native_priority(Thread* thread, int priority) {
2789  if (!UseThreadPriorities) return OS_OK;
2790  bool ret = SetThreadPriority(thread->osthread()->thread_handle(), priority) != 0;
2791  return ret ? OS_OK : OS_ERR;
2792}
2793
2794OSReturn os::get_native_priority(const Thread* const thread, int* priority_ptr) {
2795  if ( !UseThreadPriorities ) {
2796    *priority_ptr = java_to_os_priority[NormPriority];
2797    return OS_OK;
2798  }
2799  int os_prio = GetThreadPriority(thread->osthread()->thread_handle());
2800  if (os_prio == THREAD_PRIORITY_ERROR_RETURN) {
2801    assert(false, "GetThreadPriority failed");
2802    return OS_ERR;
2803  }
2804  *priority_ptr = os_prio;
2805  return OS_OK;
2806}
2807
2808
2809// Hint to the underlying OS that a task switch would not be good.
2810// Void return because it's a hint and can fail.
2811void os::hint_no_preempt() {}
2812
2813void os::interrupt(Thread* thread) {
2814  assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
2815         "possibility of dangling Thread pointer");
2816
2817  OSThread* osthread = thread->osthread();
2818  osthread->set_interrupted(true);
2819  // More than one thread can get here with the same value of osthread,
2820  // resulting in multiple notifications.  We do, however, want the store
2821  // to interrupted() to be visible to other threads before we post
2822  // the interrupt event.
2823  OrderAccess::release();
2824  SetEvent(osthread->interrupt_event());
2825  // For JSR166:  unpark after setting status
2826  if (thread->is_Java_thread())
2827    ((JavaThread*)thread)->parker()->unpark();
2828
2829  ParkEvent * ev = thread->_ParkEvent ;
2830  if (ev != NULL) ev->unpark() ;
2831
2832}
2833
2834
2835bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
2836  assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
2837         "possibility of dangling Thread pointer");
2838
2839  OSThread* osthread = thread->osthread();
2840  bool interrupted;
2841  interrupted = osthread->interrupted();
2842  if (clear_interrupted == true) {
2843    osthread->set_interrupted(false);
2844    ResetEvent(osthread->interrupt_event());
2845  } // Otherwise leave the interrupted state alone
2846
2847  return interrupted;
2848}
2849
2850// Get's a pc (hint) for a running thread. Currently used only for profiling.
2851ExtendedPC os::get_thread_pc(Thread* thread) {
2852  CONTEXT context;
2853  context.ContextFlags = CONTEXT_CONTROL;
2854  HANDLE handle = thread->osthread()->thread_handle();
2855#ifdef _M_IA64
2856  assert(0, "Fix get_thread_pc");
2857  return ExtendedPC(NULL);
2858#else
2859  if (GetThreadContext(handle, &context)) {
2860#ifdef _M_AMD64
2861    return ExtendedPC((address) context.Rip);
2862#else
2863    return ExtendedPC((address) context.Eip);
2864#endif
2865  } else {
2866    return ExtendedPC(NULL);
2867  }
2868#endif
2869}
2870
2871// GetCurrentThreadId() returns DWORD
2872intx os::current_thread_id()          { return GetCurrentThreadId(); }
2873
2874static int _initial_pid = 0;
2875
2876int os::current_process_id()
2877{
2878  return (_initial_pid ? _initial_pid : _getpid());
2879}
2880
2881int    os::win32::_vm_page_size       = 0;
2882int    os::win32::_vm_allocation_granularity = 0;
2883int    os::win32::_processor_type     = 0;
2884// Processor level is not available on non-NT systems, use vm_version instead
2885int    os::win32::_processor_level    = 0;
2886julong os::win32::_physical_memory    = 0;
2887size_t os::win32::_default_stack_size = 0;
2888
2889         intx os::win32::_os_thread_limit    = 0;
2890volatile intx os::win32::_os_thread_count    = 0;
2891
2892bool   os::win32::_is_nt              = false;
2893
2894
2895void os::win32::initialize_system_info() {
2896  SYSTEM_INFO si;
2897  GetSystemInfo(&si);
2898  _vm_page_size    = si.dwPageSize;
2899  _vm_allocation_granularity = si.dwAllocationGranularity;
2900  _processor_type  = si.dwProcessorType;
2901  _processor_level = si.wProcessorLevel;
2902  _processor_count = si.dwNumberOfProcessors;
2903
2904  MEMORYSTATUS ms;
2905  // also returns dwAvailPhys (free physical memory bytes), dwTotalVirtual, dwAvailVirtual,
2906  // dwMemoryLoad (% of memory in use)
2907  GlobalMemoryStatus(&ms);
2908  _physical_memory = ms.dwTotalPhys;
2909
2910  OSVERSIONINFO oi;
2911  oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
2912  GetVersionEx(&oi);
2913  switch(oi.dwPlatformId) {
2914    case VER_PLATFORM_WIN32_WINDOWS: _is_nt = false; break;
2915    case VER_PLATFORM_WIN32_NT:      _is_nt = true;  break;
2916    default: fatal("Unknown platform");
2917  }
2918
2919  _default_stack_size = os::current_stack_size();
2920  assert(_default_stack_size > (size_t) _vm_page_size, "invalid stack size");
2921  assert((_default_stack_size & (_vm_page_size - 1)) == 0,
2922    "stack size not a multiple of page size");
2923
2924  initialize_performance_counter();
2925
2926  // Win95/Win98 scheduler bug work-around. The Win95/98 scheduler is
2927  // known to deadlock the system, if the VM issues to thread operations with
2928  // a too high frequency, e.g., such as changing the priorities.
2929  // The 6000 seems to work well - no deadlocks has been notices on the test
2930  // programs that we have seen experience this problem.
2931  if (!os::win32::is_nt()) {
2932    StarvationMonitorInterval = 6000;
2933  }
2934}
2935
2936
2937void os::win32::setmode_streams() {
2938  _setmode(_fileno(stdin), _O_BINARY);
2939  _setmode(_fileno(stdout), _O_BINARY);
2940  _setmode(_fileno(stderr), _O_BINARY);
2941}
2942
2943
2944int os::message_box(const char* title, const char* message) {
2945  int result = MessageBox(NULL, message, title,
2946                          MB_YESNO | MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFAULT_DESKTOP_ONLY);
2947  return result == IDYES;
2948}
2949
2950int os::allocate_thread_local_storage() {
2951  return TlsAlloc();
2952}
2953
2954
2955void os::free_thread_local_storage(int index) {
2956  TlsFree(index);
2957}
2958
2959
2960void os::thread_local_storage_at_put(int index, void* value) {
2961  TlsSetValue(index, value);
2962  assert(thread_local_storage_at(index) == value, "Just checking");
2963}
2964
2965
2966void* os::thread_local_storage_at(int index) {
2967  return TlsGetValue(index);
2968}
2969
2970
2971#ifndef PRODUCT
2972#ifndef _WIN64
2973// Helpers to check whether NX protection is enabled
2974int nx_exception_filter(_EXCEPTION_POINTERS *pex) {
2975  if (pex->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
2976      pex->ExceptionRecord->NumberParameters > 0 &&
2977      pex->ExceptionRecord->ExceptionInformation[0] ==
2978      EXCEPTION_INFO_EXEC_VIOLATION) {
2979    return EXCEPTION_EXECUTE_HANDLER;
2980  }
2981  return EXCEPTION_CONTINUE_SEARCH;
2982}
2983
2984void nx_check_protection() {
2985  // If NX is enabled we'll get an exception calling into code on the stack
2986  char code[] = { (char)0xC3 }; // ret
2987  void *code_ptr = (void *)code;
2988  __try {
2989    __asm call code_ptr
2990  } __except(nx_exception_filter((_EXCEPTION_POINTERS*)_exception_info())) {
2991    tty->print_raw_cr("NX protection detected.");
2992  }
2993}
2994#endif // _WIN64
2995#endif // PRODUCT
2996
2997// this is called _before_ the global arguments have been parsed
2998void os::init(void) {
2999  _initial_pid = _getpid();
3000
3001  init_random(1234567);
3002
3003  win32::initialize_system_info();
3004  win32::setmode_streams();
3005  init_page_sizes((size_t) win32::vm_page_size());
3006
3007  // For better scalability on MP systems (must be called after initialize_system_info)
3008#ifndef PRODUCT
3009  if (is_MP()) {
3010    NoYieldsInMicrolock = true;
3011  }
3012#endif
3013  // Initialize main_process and main_thread
3014  main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
3015  if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
3016                       &main_thread, THREAD_ALL_ACCESS, false, 0)) {
3017    fatal("DuplicateHandle failed\n");
3018  }
3019  main_thread_id = (int) GetCurrentThreadId();
3020}
3021
3022// To install functions for atexit processing
3023extern "C" {
3024  static void perfMemory_exit_helper() {
3025    perfMemory_exit();
3026  }
3027}
3028
3029
3030// this is called _after_ the global arguments have been parsed
3031jint os::init_2(void) {
3032  // Allocate a single page and mark it as readable for safepoint polling
3033  address polling_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READONLY);
3034  guarantee( polling_page != NULL, "Reserve Failed for polling page");
3035
3036  address return_page  = (address)VirtualAlloc(polling_page, os::vm_page_size(), MEM_COMMIT, PAGE_READONLY);
3037  guarantee( return_page != NULL, "Commit Failed for polling page");
3038
3039  os::set_polling_page( polling_page );
3040
3041#ifndef PRODUCT
3042  if( Verbose && PrintMiscellaneous )
3043    tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n", (intptr_t)polling_page);
3044#endif
3045
3046  if (!UseMembar) {
3047    address mem_serialize_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_EXECUTE_READWRITE);
3048    guarantee( mem_serialize_page != NULL, "Reserve Failed for memory serialize page");
3049
3050    return_page  = (address)VirtualAlloc(mem_serialize_page, os::vm_page_size(), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
3051    guarantee( return_page != NULL, "Commit Failed for memory serialize page");
3052
3053    os::set_memory_serialize_page( mem_serialize_page );
3054
3055#ifndef PRODUCT
3056    if(Verbose && PrintMiscellaneous)
3057      tty->print("[Memory Serialize  Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
3058#endif
3059}
3060
3061  FLAG_SET_DEFAULT(UseLargePages, os::large_page_init());
3062
3063  // Setup Windows Exceptions
3064
3065  // On Itanium systems, Structured Exception Handling does not
3066  // work since stack frames must be walkable by the OS.  Since
3067  // much of our code is dynamically generated, and we do not have
3068  // proper unwind .xdata sections, the system simply exits
3069  // rather than delivering the exception.  To work around
3070  // this we use VectorExceptions instead.
3071#ifdef _WIN64
3072  if (UseVectoredExceptions) {
3073    topLevelVectoredExceptionHandler = AddVectoredExceptionHandler( 1, topLevelExceptionFilter);
3074  }
3075#endif
3076
3077  // for debugging float code generation bugs
3078  if (ForceFloatExceptions) {
3079#ifndef  _WIN64
3080    static long fp_control_word = 0;
3081    __asm { fstcw fp_control_word }
3082    // see Intel PPro Manual, Vol. 2, p 7-16
3083    const long precision = 0x20;
3084    const long underflow = 0x10;
3085    const long overflow  = 0x08;
3086    const long zero_div  = 0x04;
3087    const long denorm    = 0x02;
3088    const long invalid   = 0x01;
3089    fp_control_word |= invalid;
3090    __asm { fldcw fp_control_word }
3091#endif
3092  }
3093
3094  // Initialize HPI.
3095  jint hpi_result = hpi::initialize();
3096  if (hpi_result != JNI_OK) { return hpi_result; }
3097
3098  // If stack_commit_size is 0, windows will reserve the default size,
3099  // but only commit a small portion of it.
3100  size_t stack_commit_size = round_to(ThreadStackSize*K, os::vm_page_size());
3101  size_t default_reserve_size = os::win32::default_stack_size();
3102  size_t actual_reserve_size = stack_commit_size;
3103  if (stack_commit_size < default_reserve_size) {
3104    // If stack_commit_size == 0, we want this too
3105    actual_reserve_size = default_reserve_size;
3106  }
3107
3108  JavaThread::set_stack_size_at_create(stack_commit_size);
3109
3110  // Calculate theoretical max. size of Threads to guard gainst artifical
3111  // out-of-memory situations, where all available address-space has been
3112  // reserved by thread stacks.
3113  assert(actual_reserve_size != 0, "Must have a stack");
3114
3115  // Calculate the thread limit when we should start doing Virtual Memory
3116  // banging. Currently when the threads will have used all but 200Mb of space.
3117  //
3118  // TODO: consider performing a similar calculation for commit size instead
3119  // as reserve size, since on a 64-bit platform we'll run into that more
3120  // often than running out of virtual memory space.  We can use the
3121  // lower value of the two calculations as the os_thread_limit.
3122  size_t max_address_space = ((size_t)1 << (BitsPerOop - 1)) - (200 * K * K);
3123  win32::_os_thread_limit = (intx)(max_address_space / actual_reserve_size);
3124
3125  // at exit methods are called in the reverse order of their registration.
3126  // there is no limit to the number of functions registered. atexit does
3127  // not set errno.
3128
3129  if (PerfAllowAtExitRegistration) {
3130    // only register atexit functions if PerfAllowAtExitRegistration is set.
3131    // atexit functions can be delayed until process exit time, which
3132    // can be problematic for embedded VM situations. Embedded VMs should
3133    // call DestroyJavaVM() to assure that VM resources are released.
3134
3135    // note: perfMemory_exit_helper atexit function may be removed in
3136    // the future if the appropriate cleanup code can be added to the
3137    // VM_Exit VMOperation's doit method.
3138    if (atexit(perfMemory_exit_helper) != 0) {
3139      warning("os::init_2 atexit(perfMemory_exit_helper) failed");
3140    }
3141  }
3142
3143  // initialize PSAPI or ToolHelp for fatal error handler
3144  if (win32::is_nt()) _init_psapi();
3145  else _init_toolhelp();
3146
3147#ifndef _WIN64
3148  // Print something if NX is enabled (win32 on AMD64)
3149  NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection());
3150#endif
3151
3152  // initialize thread priority policy
3153  prio_init();
3154
3155  return JNI_OK;
3156}
3157
3158
3159// Mark the polling page as unreadable
3160void os::make_polling_page_unreadable(void) {
3161  DWORD old_status;
3162  if( !VirtualProtect((char *)_polling_page, os::vm_page_size(), PAGE_NOACCESS, &old_status) )
3163    fatal("Could not disable polling page");
3164};
3165
3166// Mark the polling page as readable
3167void os::make_polling_page_readable(void) {
3168  DWORD old_status;
3169  if( !VirtualProtect((char *)_polling_page, os::vm_page_size(), PAGE_READONLY, &old_status) )
3170    fatal("Could not enable polling page");
3171};
3172
3173
3174int os::stat(const char *path, struct stat *sbuf) {
3175  char pathbuf[MAX_PATH];
3176  if (strlen(path) > MAX_PATH - 1) {
3177    errno = ENAMETOOLONG;
3178    return -1;
3179  }
3180  hpi::native_path(strcpy(pathbuf, path));
3181  int ret = ::stat(pathbuf, sbuf);
3182  if (sbuf != NULL && UseUTCFileTimestamp) {
3183    // Fix for 6539723.  st_mtime returned from stat() is dependent on
3184    // the system timezone and so can return different values for the
3185    // same file if/when daylight savings time changes.  This adjustment
3186    // makes sure the same timestamp is returned regardless of the TZ.
3187    //
3188    // See:
3189    // http://msdn.microsoft.com/library/
3190    //   default.asp?url=/library/en-us/sysinfo/base/
3191    //   time_zone_information_str.asp
3192    // and
3193    // http://msdn.microsoft.com/library/default.asp?url=
3194    //   /library/en-us/sysinfo/base/settimezoneinformation.asp
3195    //
3196    // NOTE: there is a insidious bug here:  If the timezone is changed
3197    // after the call to stat() but before 'GetTimeZoneInformation()', then
3198    // the adjustment we do here will be wrong and we'll return the wrong
3199    // value (which will likely end up creating an invalid class data
3200    // archive).  Absent a better API for this, or some time zone locking
3201    // mechanism, we'll have to live with this risk.
3202    TIME_ZONE_INFORMATION tz;
3203    DWORD tzid = GetTimeZoneInformation(&tz);
3204    int daylightBias =
3205      (tzid == TIME_ZONE_ID_DAYLIGHT) ?  tz.DaylightBias : tz.StandardBias;
3206    sbuf->st_mtime += (tz.Bias + daylightBias) * 60;
3207  }
3208  return ret;
3209}
3210
3211
3212#define FT2INT64(ft) \
3213  ((jlong)((jlong)(ft).dwHighDateTime << 32 | (julong)(ft).dwLowDateTime))
3214
3215
3216// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
3217// are used by JVM M&M and JVMTI to get user+sys or user CPU time
3218// of a thread.
3219//
3220// current_thread_cpu_time() and thread_cpu_time(Thread*) returns
3221// the fast estimate available on the platform.
3222
3223// current_thread_cpu_time() is not optimized for Windows yet
3224jlong os::current_thread_cpu_time() {
3225  // return user + sys since the cost is the same
3226  return os::thread_cpu_time(Thread::current(), true /* user+sys */);
3227}
3228
3229jlong os::thread_cpu_time(Thread* thread) {
3230  // consistent with what current_thread_cpu_time() returns.
3231  return os::thread_cpu_time(thread, true /* user+sys */);
3232}
3233
3234jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
3235  return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
3236}
3237
3238jlong os::thread_cpu_time(Thread* thread, bool user_sys_cpu_time) {
3239  // This code is copy from clasic VM -> hpi::sysThreadCPUTime
3240  // If this function changes, os::is_thread_cpu_time_supported() should too
3241  if (os::win32::is_nt()) {
3242    FILETIME CreationTime;
3243    FILETIME ExitTime;
3244    FILETIME KernelTime;
3245    FILETIME UserTime;
3246
3247    if ( GetThreadTimes(thread->osthread()->thread_handle(),
3248                    &CreationTime, &ExitTime, &KernelTime, &UserTime) == 0)
3249      return -1;
3250    else
3251      if (user_sys_cpu_time) {
3252        return (FT2INT64(UserTime) + FT2INT64(KernelTime)) * 100;
3253      } else {
3254        return FT2INT64(UserTime) * 100;
3255      }
3256  } else {
3257    return (jlong) timeGetTime() * 1000000;
3258  }
3259}
3260
3261void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
3262  info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
3263  info_ptr->may_skip_backward = false;      // GetThreadTimes returns absolute time
3264  info_ptr->may_skip_forward = false;       // GetThreadTimes returns absolute time
3265  info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
3266}
3267
3268void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
3269  info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
3270  info_ptr->may_skip_backward = false;      // GetThreadTimes returns absolute time
3271  info_ptr->may_skip_forward = false;       // GetThreadTimes returns absolute time
3272  info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
3273}
3274
3275bool os::is_thread_cpu_time_supported() {
3276  // see os::thread_cpu_time
3277  if (os::win32::is_nt()) {
3278    FILETIME CreationTime;
3279    FILETIME ExitTime;
3280    FILETIME KernelTime;
3281    FILETIME UserTime;
3282
3283    if ( GetThreadTimes(GetCurrentThread(),
3284                    &CreationTime, &ExitTime, &KernelTime, &UserTime) == 0)
3285      return false;
3286    else
3287      return true;
3288  } else {
3289    return false;
3290  }
3291}
3292
3293// Windows does't provide a loadavg primitive so this is stubbed out for now.
3294// It does have primitives (PDH API) to get CPU usage and run queue length.
3295// "\\Processor(_Total)\\% Processor Time", "\\System\\Processor Queue Length"
3296// If we wanted to implement loadavg on Windows, we have a few options:
3297//
3298// a) Query CPU usage and run queue length and "fake" an answer by
3299//    returning the CPU usage if it's under 100%, and the run queue
3300//    length otherwise.  It turns out that querying is pretty slow
3301//    on Windows, on the order of 200 microseconds on a fast machine.
3302//    Note that on the Windows the CPU usage value is the % usage
3303//    since the last time the API was called (and the first call
3304//    returns 100%), so we'd have to deal with that as well.
3305//
3306// b) Sample the "fake" answer using a sampling thread and store
3307//    the answer in a global variable.  The call to loadavg would
3308//    just return the value of the global, avoiding the slow query.
3309//
3310// c) Sample a better answer using exponential decay to smooth the
3311//    value.  This is basically the algorithm used by UNIX kernels.
3312//
3313// Note that sampling thread starvation could affect both (b) and (c).
3314int os::loadavg(double loadavg[], int nelem) {
3315  return -1;
3316}
3317
3318
3319// DontYieldALot=false by default: dutifully perform all yields as requested by JVM_Yield()
3320bool os::dont_yield() {
3321  return DontYieldALot;
3322}
3323
3324// Is a (classpath) directory empty?
3325bool os::dir_is_empty(const char* path) {
3326  WIN32_FIND_DATA fd;
3327  HANDLE f = FindFirstFile(path, &fd);
3328  if (f == INVALID_HANDLE_VALUE) {
3329    return true;
3330  }
3331  FindClose(f);
3332  return false;
3333}
3334
3335// create binary file, rewriting existing file if required
3336int os::create_binary_file(const char* path, bool rewrite_existing) {
3337  int oflags = _O_CREAT | _O_WRONLY | _O_BINARY;
3338  if (!rewrite_existing) {
3339    oflags |= _O_EXCL;
3340  }
3341  return ::open(path, oflags, _S_IREAD | _S_IWRITE);
3342}
3343
3344// return current position of file pointer
3345jlong os::current_file_offset(int fd) {
3346  return (jlong)::_lseeki64(fd, (__int64)0L, SEEK_CUR);
3347}
3348
3349// move file pointer to the specified offset
3350jlong os::seek_to_file_offset(int fd, jlong offset) {
3351  return (jlong)::_lseeki64(fd, (__int64)offset, SEEK_SET);
3352}
3353
3354
3355// Map a block of memory.
3356char* os::map_memory(int fd, const char* file_name, size_t file_offset,
3357                     char *addr, size_t bytes, bool read_only,
3358                     bool allow_exec) {
3359  HANDLE hFile;
3360  char* base;
3361
3362  hFile = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
3363                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3364  if (hFile == NULL) {
3365    if (PrintMiscellaneous && Verbose) {
3366      DWORD err = GetLastError();
3367      tty->print_cr("CreateFile() failed: GetLastError->%ld.");
3368    }
3369    return NULL;
3370  }
3371
3372  if (allow_exec) {
3373    // CreateFileMapping/MapViewOfFileEx can't map executable memory
3374    // unless it comes from a PE image (which the shared archive is not.)
3375    // Even VirtualProtect refuses to give execute access to mapped memory
3376    // that was not previously executable.
3377    //
3378    // Instead, stick the executable region in anonymous memory.  Yuck.
3379    // Penalty is that ~4 pages will not be shareable - in the future
3380    // we might consider DLLizing the shared archive with a proper PE
3381    // header so that mapping executable + sharing is possible.
3382
3383    base = (char*) VirtualAlloc(addr, bytes, MEM_COMMIT | MEM_RESERVE,
3384                                PAGE_READWRITE);
3385    if (base == NULL) {
3386      if (PrintMiscellaneous && Verbose) {
3387        DWORD err = GetLastError();
3388        tty->print_cr("VirtualAlloc() failed: GetLastError->%ld.", err);
3389      }
3390      CloseHandle(hFile);
3391      return NULL;
3392    }
3393
3394    DWORD bytes_read;
3395    OVERLAPPED overlapped;
3396    overlapped.Offset = (DWORD)file_offset;
3397    overlapped.OffsetHigh = 0;
3398    overlapped.hEvent = NULL;
3399    // ReadFile guarantees that if the return value is true, the requested
3400    // number of bytes were read before returning.
3401    bool res = ReadFile(hFile, base, (DWORD)bytes, &bytes_read, &overlapped) != 0;
3402    if (!res) {
3403      if (PrintMiscellaneous && Verbose) {
3404        DWORD err = GetLastError();
3405        tty->print_cr("ReadFile() failed: GetLastError->%ld.", err);
3406      }
3407      release_memory(base, bytes);
3408      CloseHandle(hFile);
3409      return NULL;
3410    }
3411  } else {
3412    HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_WRITECOPY, 0, 0,
3413                                    NULL /*file_name*/);
3414    if (hMap == NULL) {
3415      if (PrintMiscellaneous && Verbose) {
3416        DWORD err = GetLastError();
3417        tty->print_cr("CreateFileMapping() failed: GetLastError->%ld.");
3418      }
3419      CloseHandle(hFile);
3420      return NULL;
3421    }
3422
3423    DWORD access = read_only ? FILE_MAP_READ : FILE_MAP_COPY;
3424    base = (char*)MapViewOfFileEx(hMap, access, 0, (DWORD)file_offset,
3425                                  (DWORD)bytes, addr);
3426    if (base == NULL) {
3427      if (PrintMiscellaneous && Verbose) {
3428        DWORD err = GetLastError();
3429        tty->print_cr("MapViewOfFileEx() failed: GetLastError->%ld.", err);
3430      }
3431      CloseHandle(hMap);
3432      CloseHandle(hFile);
3433      return NULL;
3434    }
3435
3436    if (CloseHandle(hMap) == 0) {
3437      if (PrintMiscellaneous && Verbose) {
3438        DWORD err = GetLastError();
3439        tty->print_cr("CloseHandle(hMap) failed: GetLastError->%ld.", err);
3440      }
3441      CloseHandle(hFile);
3442      return base;
3443    }
3444  }
3445
3446  if (allow_exec) {
3447    DWORD old_protect;
3448    DWORD exec_access = read_only ? PAGE_EXECUTE_READ : PAGE_EXECUTE_READWRITE;
3449    bool res = VirtualProtect(base, bytes, exec_access, &old_protect) != 0;
3450
3451    if (!res) {
3452      if (PrintMiscellaneous && Verbose) {
3453        DWORD err = GetLastError();
3454        tty->print_cr("VirtualProtect() failed: GetLastError->%ld.", err);
3455      }
3456      // Don't consider this a hard error, on IA32 even if the
3457      // VirtualProtect fails, we should still be able to execute
3458      CloseHandle(hFile);
3459      return base;
3460    }
3461  }
3462
3463  if (CloseHandle(hFile) == 0) {
3464    if (PrintMiscellaneous && Verbose) {
3465      DWORD err = GetLastError();
3466      tty->print_cr("CloseHandle(hFile) failed: GetLastError->%ld.", err);
3467    }
3468    return base;
3469  }
3470
3471  return base;
3472}
3473
3474
3475// Remap a block of memory.
3476char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
3477                       char *addr, size_t bytes, bool read_only,
3478                       bool allow_exec) {
3479  // This OS does not allow existing memory maps to be remapped so we
3480  // have to unmap the memory before we remap it.
3481  if (!os::unmap_memory(addr, bytes)) {
3482    return NULL;
3483  }
3484
3485  // There is a very small theoretical window between the unmap_memory()
3486  // call above and the map_memory() call below where a thread in native
3487  // code may be able to access an address that is no longer mapped.
3488
3489  return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
3490                        allow_exec);
3491}
3492
3493
3494// Unmap a block of memory.
3495// Returns true=success, otherwise false.
3496
3497bool os::unmap_memory(char* addr, size_t bytes) {
3498  BOOL result = UnmapViewOfFile(addr);
3499  if (result == 0) {
3500    if (PrintMiscellaneous && Verbose) {
3501      DWORD err = GetLastError();
3502      tty->print_cr("UnmapViewOfFile() failed: GetLastError->%ld.", err);
3503    }
3504    return false;
3505  }
3506  return true;
3507}
3508
3509void os::pause() {
3510  char filename[MAX_PATH];
3511  if (PauseAtStartupFile && PauseAtStartupFile[0]) {
3512    jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
3513  } else {
3514    jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
3515  }
3516
3517  int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
3518  if (fd != -1) {
3519    struct stat buf;
3520    close(fd);
3521    while (::stat(filename, &buf) == 0) {
3522      Sleep(100);
3523    }
3524  } else {
3525    jio_fprintf(stderr,
3526      "Could not open pause file '%s', continuing immediately.\n", filename);
3527  }
3528}
3529
3530// An Event wraps a win32 "CreateEvent" kernel handle.
3531//
3532// We have a number of choices regarding "CreateEvent" win32 handle leakage:
3533//
3534// 1:  When a thread dies return the Event to the EventFreeList, clear the ParkHandle
3535//     field, and call CloseHandle() on the win32 event handle.  Unpark() would
3536//     need to be modified to tolerate finding a NULL (invalid) win32 event handle.
3537//     In addition, an unpark() operation might fetch the handle field, but the
3538//     event could recycle between the fetch and the SetEvent() operation.
3539//     SetEvent() would either fail because the handle was invalid, or inadvertently work,
3540//     as the win32 handle value had been recycled.  In an ideal world calling SetEvent()
3541//     on an stale but recycled handle would be harmless, but in practice this might
3542//     confuse other non-Sun code, so it's not a viable approach.
3543//
3544// 2:  Once a win32 event handle is associated with an Event, it remains associated
3545//     with the Event.  The event handle is never closed.  This could be construed
3546//     as handle leakage, but only up to the maximum # of threads that have been extant
3547//     at any one time.  This shouldn't be an issue, as windows platforms typically
3548//     permit a process to have hundreds of thousands of open handles.
3549//
3550// 3:  Same as (1), but periodically, at stop-the-world time, rundown the EventFreeList
3551//     and release unused handles.
3552//
3553// 4:  Add a CRITICAL_SECTION to the Event to protect LD+SetEvent from LD;ST(null);CloseHandle.
3554//     It's not clear, however, that we wouldn't be trading one type of leak for another.
3555//
3556// 5.  Use an RCU-like mechanism (Read-Copy Update).
3557//     Or perhaps something similar to Maged Michael's "Hazard pointers".
3558//
3559// We use (2).
3560//
3561// TODO-FIXME:
3562// 1.  Reconcile Doug's JSR166 j.u.c park-unpark with the objectmonitor implementation.
3563// 2.  Consider wrapping the WaitForSingleObject(Ex) calls in SEH try/finally blocks
3564//     to recover from (or at least detect) the dreaded Windows 841176 bug.
3565// 3.  Collapse the interrupt_event, the JSR166 parker event, and the objectmonitor ParkEvent
3566//     into a single win32 CreateEvent() handle.
3567//
3568// _Event transitions in park()
3569//   -1 => -1 : illegal
3570//    1 =>  0 : pass - return immediately
3571//    0 => -1 : block
3572//
3573// _Event serves as a restricted-range semaphore :
3574//    -1 : thread is blocked
3575//     0 : neutral  - thread is running or ready
3576//     1 : signaled - thread is running or ready
3577//
3578// Another possible encoding of _Event would be
3579// with explicit "PARKED" and "SIGNALED" bits.
3580
3581int os::PlatformEvent::park (jlong Millis) {
3582    guarantee (_ParkHandle != NULL , "Invariant") ;
3583    guarantee (Millis > 0          , "Invariant") ;
3584    int v ;
3585
3586    // CONSIDER: defer assigning a CreateEvent() handle to the Event until
3587    // the initial park() operation.
3588
3589    for (;;) {
3590        v = _Event ;
3591        if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
3592    }
3593    guarantee ((v == 0) || (v == 1), "invariant") ;
3594    if (v != 0) return OS_OK ;
3595
3596    // Do this the hard way by blocking ...
3597    // TODO: consider a brief spin here, gated on the success of recent
3598    // spin attempts by this thread.
3599    //
3600    // We decompose long timeouts into series of shorter timed waits.
3601    // Evidently large timo values passed in WaitForSingleObject() are problematic on some
3602    // versions of Windows.  See EventWait() for details.  This may be superstition.  Or not.
3603    // We trust the WAIT_TIMEOUT indication and don't track the elapsed wait time
3604    // with os::javaTimeNanos().  Furthermore, we assume that spurious returns from
3605    // ::WaitForSingleObject() caused by latent ::setEvent() operations will tend
3606    // to happen early in the wait interval.  Specifically, after a spurious wakeup (rv ==
3607    // WAIT_OBJECT_0 but _Event is still < 0) we don't bother to recompute Millis to compensate
3608    // for the already waited time.  This policy does not admit any new outcomes.
3609    // In the future, however, we might want to track the accumulated wait time and
3610    // adjust Millis accordingly if we encounter a spurious wakeup.
3611
3612    const int MAXTIMEOUT = 0x10000000 ;
3613    DWORD rv = WAIT_TIMEOUT ;
3614    while (_Event < 0 && Millis > 0) {
3615       DWORD prd = Millis ;     // set prd = MAX (Millis, MAXTIMEOUT)
3616       if (Millis > MAXTIMEOUT) {
3617          prd = MAXTIMEOUT ;
3618       }
3619       rv = ::WaitForSingleObject (_ParkHandle, prd) ;
3620       assert (rv == WAIT_OBJECT_0 || rv == WAIT_TIMEOUT, "WaitForSingleObject failed") ;
3621       if (rv == WAIT_TIMEOUT) {
3622           Millis -= prd ;
3623       }
3624    }
3625    v = _Event ;
3626    _Event = 0 ;
3627    OrderAccess::fence() ;
3628    // If we encounter a nearly simultanous timeout expiry and unpark()
3629    // we return OS_OK indicating we awoke via unpark().
3630    // Implementor's license -- returning OS_TIMEOUT would be equally valid, however.
3631    return (v >= 0) ? OS_OK : OS_TIMEOUT ;
3632}
3633
3634void os::PlatformEvent::park () {
3635    guarantee (_ParkHandle != NULL, "Invariant") ;
3636    // Invariant: Only the thread associated with the Event/PlatformEvent
3637    // may call park().
3638    int v ;
3639    for (;;) {
3640        v = _Event ;
3641        if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
3642    }
3643    guarantee ((v == 0) || (v == 1), "invariant") ;
3644    if (v != 0) return ;
3645
3646    // Do this the hard way by blocking ...
3647    // TODO: consider a brief spin here, gated on the success of recent
3648    // spin attempts by this thread.
3649    while (_Event < 0) {
3650       DWORD rv = ::WaitForSingleObject (_ParkHandle, INFINITE) ;
3651       assert (rv == WAIT_OBJECT_0, "WaitForSingleObject failed") ;
3652    }
3653
3654    // Usually we'll find _Event == 0 at this point, but as
3655    // an optional optimization we clear it, just in case can
3656    // multiple unpark() operations drove _Event up to 1.
3657    _Event = 0 ;
3658    OrderAccess::fence() ;
3659    guarantee (_Event >= 0, "invariant") ;
3660}
3661
3662void os::PlatformEvent::unpark() {
3663  guarantee (_ParkHandle != NULL, "Invariant") ;
3664  int v ;
3665  for (;;) {
3666      v = _Event ;      // Increment _Event if it's < 1.
3667      if (v > 0) {
3668         // If it's already signaled just return.
3669         // The LD of _Event could have reordered or be satisfied
3670         // by a read-aside from this processor's write buffer.
3671         // To avoid problems execute a barrier and then
3672         // ratify the value.  A degenerate CAS() would also work.
3673         // Viz., CAS (v+0, &_Event, v) == v).
3674         OrderAccess::fence() ;
3675         if (_Event == v) return ;
3676         continue ;
3677      }
3678      if (Atomic::cmpxchg (v+1, &_Event, v) == v) break ;
3679  }
3680  if (v < 0) {
3681     ::SetEvent (_ParkHandle) ;
3682  }
3683}
3684
3685
3686// JSR166
3687// -------------------------------------------------------
3688
3689/*
3690 * The Windows implementation of Park is very straightforward: Basic
3691 * operations on Win32 Events turn out to have the right semantics to
3692 * use them directly. We opportunistically resuse the event inherited
3693 * from Monitor.
3694 */
3695
3696
3697void Parker::park(bool isAbsolute, jlong time) {
3698  guarantee (_ParkEvent != NULL, "invariant") ;
3699  // First, demultiplex/decode time arguments
3700  if (time < 0) { // don't wait
3701    return;
3702  }
3703  else if (time == 0) {
3704    time = INFINITE;
3705  }
3706  else if  (isAbsolute) {
3707    time -= os::javaTimeMillis(); // convert to relative time
3708    if (time <= 0) // already elapsed
3709      return;
3710  }
3711  else { // relative
3712    time /= 1000000; // Must coarsen from nanos to millis
3713    if (time == 0)   // Wait for the minimal time unit if zero
3714      time = 1;
3715  }
3716
3717  JavaThread* thread = (JavaThread*)(Thread::current());
3718  assert(thread->is_Java_thread(), "Must be JavaThread");
3719  JavaThread *jt = (JavaThread *)thread;
3720
3721  // Don't wait if interrupted or already triggered
3722  if (Thread::is_interrupted(thread, false) ||
3723    WaitForSingleObject(_ParkEvent, 0) == WAIT_OBJECT_0) {
3724    ResetEvent(_ParkEvent);
3725    return;
3726  }
3727  else {
3728    ThreadBlockInVM tbivm(jt);
3729    OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
3730    jt->set_suspend_equivalent();
3731
3732    WaitForSingleObject(_ParkEvent,  time);
3733    ResetEvent(_ParkEvent);
3734
3735    // If externally suspended while waiting, re-suspend
3736    if (jt->handle_special_suspend_equivalent_condition()) {
3737      jt->java_suspend_self();
3738    }
3739  }
3740}
3741
3742void Parker::unpark() {
3743  guarantee (_ParkEvent != NULL, "invariant") ;
3744  SetEvent(_ParkEvent);
3745}
3746
3747// Run the specified command in a separate process. Return its exit value,
3748// or -1 on failure (e.g. can't create a new process).
3749int os::fork_and_exec(char* cmd) {
3750  STARTUPINFO si;
3751  PROCESS_INFORMATION pi;
3752
3753  memset(&si, 0, sizeof(si));
3754  si.cb = sizeof(si);
3755  memset(&pi, 0, sizeof(pi));
3756  BOOL rslt = CreateProcess(NULL,   // executable name - use command line
3757                            cmd,    // command line
3758                            NULL,   // process security attribute
3759                            NULL,   // thread security attribute
3760                            TRUE,   // inherits system handles
3761                            0,      // no creation flags
3762                            NULL,   // use parent's environment block
3763                            NULL,   // use parent's starting directory
3764                            &si,    // (in) startup information
3765                            &pi);   // (out) process information
3766
3767  if (rslt) {
3768    // Wait until child process exits.
3769    WaitForSingleObject(pi.hProcess, INFINITE);
3770
3771    DWORD exit_code;
3772    GetExitCodeProcess(pi.hProcess, &exit_code);
3773
3774    // Close process and thread handles.
3775    CloseHandle(pi.hProcess);
3776    CloseHandle(pi.hThread);
3777
3778    return (int)exit_code;
3779  } else {
3780    return -1;
3781  }
3782}
3783
3784//--------------------------------------------------------------------------------------------------
3785// Non-product code
3786
3787static int mallocDebugIntervalCounter = 0;
3788static int mallocDebugCounter = 0;
3789bool os::check_heap(bool force) {
3790  if (++mallocDebugCounter < MallocVerifyStart && !force) return true;
3791  if (++mallocDebugIntervalCounter >= MallocVerifyInterval || force) {
3792    // Note: HeapValidate executes two hardware breakpoints when it finds something
3793    // wrong; at these points, eax contains the address of the offending block (I think).
3794    // To get to the exlicit error message(s) below, just continue twice.
3795    HANDLE heap = GetProcessHeap();
3796    { HeapLock(heap);
3797      PROCESS_HEAP_ENTRY phe;
3798      phe.lpData = NULL;
3799      while (HeapWalk(heap, &phe) != 0) {
3800        if ((phe.wFlags & PROCESS_HEAP_ENTRY_BUSY) &&
3801            !HeapValidate(heap, 0, phe.lpData)) {
3802          tty->print_cr("C heap has been corrupted (time: %d allocations)", mallocDebugCounter);
3803          tty->print_cr("corrupted block near address %#x, length %d", phe.lpData, phe.cbData);
3804          fatal("corrupted C heap");
3805        }
3806      }
3807      int err = GetLastError();
3808      if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) {
3809        fatal1("heap walk aborted with error %d", err);
3810      }
3811      HeapUnlock(heap);
3812    }
3813    mallocDebugIntervalCounter = 0;
3814  }
3815  return true;
3816}
3817
3818
3819#ifndef PRODUCT
3820bool os::find(address addr) {
3821  // Nothing yet
3822  return false;
3823}
3824#endif
3825
3826LONG WINAPI os::win32::serialize_fault_filter(struct _EXCEPTION_POINTERS* e) {
3827  DWORD exception_code = e->ExceptionRecord->ExceptionCode;
3828
3829  if ( exception_code == EXCEPTION_ACCESS_VIOLATION ) {
3830    JavaThread* thread = (JavaThread*)ThreadLocalStorage::get_thread_slow();
3831    PEXCEPTION_RECORD exceptionRecord = e->ExceptionRecord;
3832    address addr = (address) exceptionRecord->ExceptionInformation[1];
3833
3834    if (os::is_memory_serialize_page(thread, addr))
3835      return EXCEPTION_CONTINUE_EXECUTION;
3836  }
3837
3838  return EXCEPTION_CONTINUE_SEARCH;
3839}
3840
3841static int getLastErrorString(char *buf, size_t len)
3842{
3843    long errval;
3844
3845    if ((errval = GetLastError()) != 0)
3846    {
3847      /* DOS error */
3848      size_t n = (size_t)FormatMessage(
3849            FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
3850            NULL,
3851            errval,
3852            0,
3853            buf,
3854            (DWORD)len,
3855            NULL);
3856      if (n > 3) {
3857        /* Drop final '.', CR, LF */
3858        if (buf[n - 1] == '\n') n--;
3859        if (buf[n - 1] == '\r') n--;
3860        if (buf[n - 1] == '.') n--;
3861        buf[n] = '\0';
3862      }
3863      return (int)n;
3864    }
3865
3866    if (errno != 0)
3867    {
3868      /* C runtime error that has no corresponding DOS error code */
3869      const char *s = strerror(errno);
3870      size_t n = strlen(s);
3871      if (n >= len) n = len - 1;
3872      strncpy(buf, s, n);
3873      buf[n] = '\0';
3874      return (int)n;
3875    }
3876    return 0;
3877}
3878