hotspot/src/os/windows/vm/os_windows.cpp
changeset 35212 7a6d0993a080
parent 35201 996db89f378e
child 35462 a1bed49c67a8
equal deleted inserted replaced
35209:b2f409087683 35212:7a6d0993a080
   318 //
   318 //
   319 int os::get_native_stack(address* stack, int frames, int toSkip) {
   319 int os::get_native_stack(address* stack, int frames, int toSkip) {
   320 #ifdef _NMT_NOINLINE_
   320 #ifdef _NMT_NOINLINE_
   321   toSkip++;
   321   toSkip++;
   322 #endif
   322 #endif
   323   int captured = Kernel32Dll::RtlCaptureStackBackTrace(toSkip + 1, frames,
   323   int captured = RtlCaptureStackBackTrace(toSkip + 1, frames, (PVOID*)stack, NULL);
   324                                                        (PVOID*)stack, NULL);
       
   325   for (int index = captured; index < frames; index ++) {
   324   for (int index = captured; index < frames; index ++) {
   326     stack[index] = NULL;
   325     stack[index] = NULL;
   327   }
   326   }
   328   return captured;
   327   return captured;
   329 }
   328 }
   595   // for CreateThread() that can treat 'stack_size' as stack size. However we
   594   // for CreateThread() that can treat 'stack_size' as stack size. However we
   596   // are not supposed to call CreateThread() directly according to MSDN
   595   // are not supposed to call CreateThread() directly according to MSDN
   597   // document because JVM uses C runtime library. The good news is that the
   596   // document because JVM uses C runtime library. The good news is that the
   598   // flag appears to work with _beginthredex() as well.
   597   // flag appears to work with _beginthredex() as well.
   599 
   598 
   600 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
       
   601   #define STACK_SIZE_PARAM_IS_A_RESERVATION  (0x10000)
       
   602 #endif
       
   603 
       
   604   HANDLE thread_handle =
   599   HANDLE thread_handle =
   605     (HANDLE)_beginthreadex(NULL,
   600     (HANDLE)_beginthreadex(NULL,
   606                            (unsigned)stack_size,
   601                            (unsigned)stack_size,
   607                            (unsigned (__stdcall *)(void*)) java_start,
   602                            (unsigned (__stdcall *)(void*)) java_start,
   608                            thread,
   603                            thread,
   609                            CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
   604                            CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
   610                            &thread_id);
   605                            &thread_id);
   611   if (thread_handle == NULL) {
   606 
   612     // perhaps STACK_SIZE_PARAM_IS_A_RESERVATION is not supported, try again
       
   613     // without the flag.
       
   614     thread_handle =
       
   615       (HANDLE)_beginthreadex(NULL,
       
   616                              (unsigned)stack_size,
       
   617                              (unsigned (__stdcall *)(void*)) java_start,
       
   618                              thread,
       
   619                              CREATE_SUSPENDED,
       
   620                              &thread_id);
       
   621   }
       
   622   if (thread_handle == NULL) {
   607   if (thread_handle == NULL) {
   623     // Need to clean up stuff we've allocated so far
   608     // Need to clean up stuff we've allocated so far
   624     CloseHandle(osthread->interrupt_event());
   609     CloseHandle(osthread->interrupt_event());
   625     thread->set_osthread(NULL);
   610     thread->set_osthread(NULL);
   626     delete osthread;
   611     delete osthread;
   662 }
   647 }
   663 
   648 
   664 
   649 
   665 jlong os::elapsed_counter() {
   650 jlong os::elapsed_counter() {
   666   LARGE_INTEGER count;
   651   LARGE_INTEGER count;
   667   if (win32::_has_performance_count) {
   652   QueryPerformanceCounter(&count);
   668     QueryPerformanceCounter(&count);
   653   return as_long(count) - initial_performance_count;
   669     return as_long(count) - initial_performance_count;
       
   670   } else {
       
   671     FILETIME wt;
       
   672     GetSystemTimeAsFileTime(&wt);
       
   673     return (jlong_from(wt.dwHighDateTime, wt.dwLowDateTime) - first_filetime);
       
   674   }
       
   675 }
   654 }
   676 
   655 
   677 
   656 
   678 jlong os::elapsed_frequency() {
   657 jlong os::elapsed_frequency() {
   679   if (win32::_has_performance_count) {
   658   return performance_frequency;
   680     return performance_frequency;
       
   681   } else {
       
   682     // the FILETIME time is the number of 100-nanosecond intervals since January 1,1601.
       
   683     return 10000000;
       
   684   }
       
   685 }
   659 }
   686 
   660 
   687 
   661 
   688 julong os::available_memory() {
   662 julong os::available_memory() {
   689   return win32::available_memory();
   663   return win32::available_memory();
   715   *limit = MIN2((julong)1400*M, (julong)ms.ullAvailVirtual);
   689   *limit = MIN2((julong)1400*M, (julong)ms.ullAvailVirtual);
   716   return true;
   690   return true;
   717 #endif
   691 #endif
   718 }
   692 }
   719 
   693 
   720 // VC6 lacks DWORD_PTR
       
   721 #if _MSC_VER < 1300
       
   722 typedef UINT_PTR DWORD_PTR;
       
   723 #endif
       
   724 
       
   725 int os::active_processor_count() {
   694 int os::active_processor_count() {
   726   DWORD_PTR lpProcessAffinityMask = 0;
   695   DWORD_PTR lpProcessAffinityMask = 0;
   727   DWORD_PTR lpSystemAffinityMask = 0;
   696   DWORD_PTR lpSystemAffinityMask = 0;
   728   int proc_count = processor_count();
   697   int proc_count = processor_count();
   729   if (proc_count <= sizeof(UINT_PTR) * BitsPerByte &&
   698   if (proc_count <= sizeof(UINT_PTR) * BitsPerByte &&
   776   return false;
   745   return false;
   777 }
   746 }
   778 
   747 
   779 void os::win32::initialize_performance_counter() {
   748 void os::win32::initialize_performance_counter() {
   780   LARGE_INTEGER count;
   749   LARGE_INTEGER count;
   781   if (QueryPerformanceFrequency(&count)) {
   750   QueryPerformanceFrequency(&count);
   782     win32::_has_performance_count = 1;
   751   performance_frequency = as_long(count);
   783     performance_frequency = as_long(count);
   752   QueryPerformanceCounter(&count);
   784     QueryPerformanceCounter(&count);
   753   initial_performance_count = as_long(count);
   785     initial_performance_count = as_long(count);
       
   786   } else {
       
   787     win32::_has_performance_count = 0;
       
   788     FILETIME wt;
       
   789     GetSystemTimeAsFileTime(&wt);
       
   790     first_filetime = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
       
   791   }
       
   792 }
   754 }
   793 
   755 
   794 
   756 
   795 double os::elapsedTime() {
   757 double os::elapsedTime() {
   796   return (double) elapsed_counter() / (double) elapsed_frequency();
   758   return (double) elapsed_counter() / (double) elapsed_frequency();
   892   seconds = secs;
   854   seconds = secs;
   893   nanos = jlong(ticks - (secs*10000000)) * 100;
   855   nanos = jlong(ticks - (secs*10000000)) * 100;
   894 }
   856 }
   895 
   857 
   896 jlong os::javaTimeNanos() {
   858 jlong os::javaTimeNanos() {
   897   if (!win32::_has_performance_count) {
       
   898     return javaTimeMillis() * NANOSECS_PER_MILLISEC; // the best we can do.
       
   899   } else {
       
   900     LARGE_INTEGER current_count;
   859     LARGE_INTEGER current_count;
   901     QueryPerformanceCounter(&current_count);
   860     QueryPerformanceCounter(&current_count);
   902     double current = as_long(current_count);
   861     double current = as_long(current_count);
   903     double freq = performance_frequency;
   862     double freq = performance_frequency;
   904     jlong time = (jlong)((current/freq) * NANOSECS_PER_SEC);
   863     jlong time = (jlong)((current/freq) * NANOSECS_PER_SEC);
   905     return time;
   864     return time;
   906   }
       
   907 }
   865 }
   908 
   866 
   909 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
   867 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
   910   if (!win32::_has_performance_count) {
   868   jlong freq = performance_frequency;
   911     // javaTimeMillis() doesn't have much percision,
   869   if (freq < NANOSECS_PER_SEC) {
   912     // but it is not going to wrap -- so all 64 bits
   870     // the performance counter is 64 bits and we will
       
   871     // be multiplying it -- so no wrap in 64 bits
   913     info_ptr->max_value = ALL_64_BITS;
   872     info_ptr->max_value = ALL_64_BITS;
   914 
   873   } else if (freq > NANOSECS_PER_SEC) {
   915     // this is a wall clock timer, so may skip
   874     // use the max value the counter can reach to
   916     info_ptr->may_skip_backward = true;
   875     // determine the max value which could be returned
   917     info_ptr->may_skip_forward = true;
   876     julong max_counter = (julong)ALL_64_BITS;
       
   877     info_ptr->max_value = (jlong)(max_counter / (freq / NANOSECS_PER_SEC));
   918   } else {
   878   } else {
   919     jlong freq = performance_frequency;
   879     // the performance counter is 64 bits and we will
   920     if (freq < NANOSECS_PER_SEC) {
   880     // be using it directly -- so no wrap in 64 bits
   921       // the performance counter is 64 bits and we will
   881     info_ptr->max_value = ALL_64_BITS;
   922       // be multiplying it -- so no wrap in 64 bits
   882   }
   923       info_ptr->max_value = ALL_64_BITS;
   883 
   924     } else if (freq > NANOSECS_PER_SEC) {
   884   // using a counter, so no skipping
   925       // use the max value the counter can reach to
   885   info_ptr->may_skip_backward = false;
   926       // determine the max value which could be returned
   886   info_ptr->may_skip_forward = false;
   927       julong max_counter = (julong)ALL_64_BITS;
   887 
   928       info_ptr->max_value = (jlong)(max_counter / (freq / NANOSECS_PER_SEC));
       
   929     } else {
       
   930       // the performance counter is 64 bits and we will
       
   931       // be using it directly -- so no wrap in 64 bits
       
   932       info_ptr->max_value = ALL_64_BITS;
       
   933     }
       
   934 
       
   935     // using a counter, so no skipping
       
   936     info_ptr->may_skip_backward = false;
       
   937     info_ptr->may_skip_forward = false;
       
   938   }
       
   939   info_ptr->kind = JVMTI_TIMER_ELAPSED;                // elapsed not CPU time
   888   info_ptr->kind = JVMTI_TIMER_ELAPSED;                // elapsed not CPU time
   940 }
   889 }
   941 
   890 
   942 char* os::local_time_string(char *buf, size_t buflen) {
   891 char* os::local_time_string(char *buf, size_t buflen) {
   943   SYSTEMTIME st;
   892   SYSTEMTIME st;
  1066     jio_fprintf(stderr, "Failed to find MiniDumpWriteDump() in module dbghelp.dll.\n");
  1015     jio_fprintf(stderr, "Failed to find MiniDumpWriteDump() in module dbghelp.dll.\n");
  1067     CloseHandle(dumpFile);
  1016     CloseHandle(dumpFile);
  1068     win32::exit_process_or_thread(win32::EPT_PROCESS, 1);
  1017     win32::exit_process_or_thread(win32::EPT_PROCESS, 1);
  1069   }
  1018   }
  1070 
  1019 
  1071   dumpType = (MINIDUMP_TYPE)(MiniDumpWithFullMemory | MiniDumpWithHandleData);
  1020   dumpType = (MINIDUMP_TYPE)(MiniDumpWithFullMemory | MiniDumpWithHandleData |
  1072 
  1021     MiniDumpWithFullMemoryInfo | MiniDumpWithThreadInfo | MiniDumpWithUnloadedModules);
  1073   // Older versions of dbghelp.h do not contain all the dumptypes we want, dbghelp.h with
       
  1074   // API_VERSION_NUMBER 11 or higher contains the ones we want though
       
  1075 #if API_VERSION_NUMBER >= 11
       
  1076   dumpType = (MINIDUMP_TYPE)(dumpType | MiniDumpWithFullMemoryInfo | MiniDumpWithThreadInfo |
       
  1077                              MiniDumpWithUnloadedModules);
       
  1078 #endif
       
  1079 
  1022 
  1080   if (siginfo != NULL && context != NULL) {
  1023   if (siginfo != NULL && context != NULL) {
  1081     ep.ContextRecord = (PCONTEXT) context;
  1024     ep.ContextRecord = (PCONTEXT) context;
  1082     ep.ExceptionRecord = (PEXCEPTION_RECORD) siginfo;
  1025     ep.ExceptionRecord = (PEXCEPTION_RECORD) siginfo;
  1083 
  1026 
  1306   HMODULE hmod;
  1249   HMODULE hmod;
  1307   MODULEINFO minfo;
  1250   MODULEINFO minfo;
  1308 
  1251 
  1309   hmod = GetModuleHandle("NTDLL.DLL");
  1252   hmod = GetModuleHandle("NTDLL.DLL");
  1310   if (hmod == NULL) return false;
  1253   if (hmod == NULL) return false;
  1311   if (!os::PSApiDll::GetModuleInformation(GetCurrentProcess(), hmod,
  1254   if (!GetModuleInformation(GetCurrentProcess(), hmod,
  1312                                           &minfo, sizeof(MODULEINFO))) {
  1255                                           &minfo, sizeof(MODULEINFO))) {
  1313     return false;
  1256     return false;
  1314   }
  1257   }
  1315 
  1258 
  1316   if ((addr >= minfo.lpBaseOfDll) &&
  1259   if ((addr >= minfo.lpBaseOfDll) &&
  1550 # define MAX_NUM_MODULES 128
  1493 # define MAX_NUM_MODULES 128
  1551   HMODULE     modules[MAX_NUM_MODULES];
  1494   HMODULE     modules[MAX_NUM_MODULES];
  1552   static char filename[MAX_PATH];
  1495   static char filename[MAX_PATH];
  1553   int         result = 0;
  1496   int         result = 0;
  1554 
  1497 
  1555   if (!os::PSApiDll::PSApiAvailable()) {
       
  1556     return 0;
       
  1557   }
       
  1558 
       
  1559   int pid = os::current_process_id();
  1498   int pid = os::current_process_id();
  1560   hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  1499   hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  1561                          FALSE, pid);
  1500                          FALSE, pid);
  1562   if (hProcess == NULL) return 0;
  1501   if (hProcess == NULL) return 0;
  1563 
  1502 
  1564   DWORD size_needed;
  1503   DWORD size_needed;
  1565   if (!os::PSApiDll::EnumProcessModules(hProcess, modules,
  1504   if (!EnumProcessModules(hProcess, modules, sizeof(modules), &size_needed)) {
  1566                                         sizeof(modules), &size_needed)) {
       
  1567     CloseHandle(hProcess);
  1505     CloseHandle(hProcess);
  1568     return 0;
  1506     return 0;
  1569   }
  1507   }
  1570 
  1508 
  1571   // number of modules that are currently loaded
  1509   // number of modules that are currently loaded
  1572   int num_modules = size_needed / sizeof(HMODULE);
  1510   int num_modules = size_needed / sizeof(HMODULE);
  1573 
  1511 
  1574   for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
  1512   for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
  1575     // Get Full pathname:
  1513     // Get Full pathname:
  1576     if (!os::PSApiDll::GetModuleFileNameEx(hProcess, modules[i],
  1514     if (!GetModuleFileNameEx(hProcess, modules[i], filename, sizeof(filename))) {
  1577                                            filename, sizeof(filename))) {
       
  1578       filename[0] = '\0';
  1515       filename[0] = '\0';
  1579     }
  1516     }
  1580 
  1517 
  1581     MODULEINFO modinfo;
  1518     MODULEINFO modinfo;
  1582     if (!os::PSApiDll::GetModuleInformation(hProcess, modules[i],
  1519     if (!GetModuleInformation(hProcess, modules[i], &modinfo, sizeof(modinfo))) {
  1583                                             &modinfo, sizeof(modinfo))) {
       
  1584       modinfo.lpBaseOfDll = NULL;
  1520       modinfo.lpBaseOfDll = NULL;
  1585       modinfo.SizeOfImage = 0;
  1521       modinfo.SizeOfImage = 0;
  1586     }
  1522     }
  1587 
  1523 
  1588     // Invoke callback function
  1524     // Invoke callback function
  1747 
  1683 
  1748   // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could
  1684   // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could
  1749   // find out whether we are running on 64 bit processor or not
  1685   // find out whether we are running on 64 bit processor or not
  1750   SYSTEM_INFO si;
  1686   SYSTEM_INFO si;
  1751   ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1687   ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1752   os::Kernel32Dll::GetNativeSystemInfo(&si);
  1688   GetNativeSystemInfo(&si);
  1753   if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
  1689   if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
  1754     st->print(" , 64 bit");
  1690     st->print(" , 64 bit");
  1755   }
  1691   }
  1756 
  1692 
  1757   st->print(" Build %d", build_number);
  1693   st->print(" Build %d", build_number);
  2534     JavaThread* thread = (JavaThread*) t;
  2470     JavaThread* thread = (JavaThread*) t;
  2535     bool in_java = thread->thread_state() == _thread_in_Java;
  2471     bool in_java = thread->thread_state() == _thread_in_Java;
  2536 
  2472 
  2537     // Handle potential stack overflows up front.
  2473     // Handle potential stack overflows up front.
  2538     if (exception_code == EXCEPTION_STACK_OVERFLOW) {
  2474     if (exception_code == EXCEPTION_STACK_OVERFLOW) {
  2539       if (os::uses_stack_guard_pages()) {
       
  2540 #ifdef _M_IA64
  2475 #ifdef _M_IA64
  2541         // Use guard page for register stack.
  2476       // Use guard page for register stack.
  2542         PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
  2477       PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
  2543         address addr = (address) exceptionRecord->ExceptionInformation[1];
  2478       address addr = (address) exceptionRecord->ExceptionInformation[1];
  2544         // Check for a register stack overflow on Itanium
  2479       // Check for a register stack overflow on Itanium
  2545         if (thread->addr_inside_register_stack_red_zone(addr)) {
  2480       if (thread->addr_inside_register_stack_red_zone(addr)) {
  2546           // Fatal red zone violation happens if the Java program
  2481         // Fatal red zone violation happens if the Java program
  2547           // catches a StackOverflow error and does so much processing
  2482         // catches a StackOverflow error and does so much processing
  2548           // that it runs beyond the unprotected yellow guard zone. As
  2483         // that it runs beyond the unprotected yellow guard zone. As
  2549           // a result, we are out of here.
  2484         // a result, we are out of here.
  2550           fatal("ERROR: Unrecoverable stack overflow happened. JVM will exit.");
  2485         fatal("ERROR: Unrecoverable stack overflow happened. JVM will exit.");
  2551         } else if(thread->addr_inside_register_stack(addr)) {
  2486       } else if(thread->addr_inside_register_stack(addr)) {
  2552           // Disable the yellow zone which sets the state that
  2487         // Disable the yellow zone which sets the state that
  2553           // we've got a stack overflow problem.
  2488         // we've got a stack overflow problem.
  2554           if (thread->stack_yellow_reserved_zone_enabled()) {
  2489         if (thread->stack_yellow_reserved_zone_enabled()) {
  2555             thread->disable_stack_yellow_reserved_zone();
  2490           thread->disable_stack_yellow_reserved_zone();
  2556           }
       
  2557           // Give us some room to process the exception.
       
  2558           thread->disable_register_stack_guard();
       
  2559           // Tracing with +Verbose.
       
  2560           if (Verbose) {
       
  2561             tty->print_cr("SOF Compiled Register Stack overflow at " INTPTR_FORMAT " (SIGSEGV)", pc);
       
  2562             tty->print_cr("Register Stack access at " INTPTR_FORMAT, addr);
       
  2563             tty->print_cr("Register Stack base " INTPTR_FORMAT, thread->register_stack_base());
       
  2564             tty->print_cr("Register Stack [" INTPTR_FORMAT "," INTPTR_FORMAT "]",
       
  2565                           thread->register_stack_base(),
       
  2566                           thread->register_stack_base() + thread->stack_size());
       
  2567           }
       
  2568 
       
  2569           // Reguard the permanent register stack red zone just to be sure.
       
  2570           // We saw Windows silently disabling this without telling us.
       
  2571           thread->enable_register_stack_red_zone();
       
  2572 
       
  2573           return Handle_Exception(exceptionInfo,
       
  2574                                   SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
       
  2575         }
  2491         }
  2576 #endif
  2492         // Give us some room to process the exception.
  2577         if (thread->stack_guards_enabled()) {
  2493         thread->disable_register_stack_guard();
  2578           if (_thread_in_Java) {
  2494         // Tracing with +Verbose.
  2579             frame fr;
  2495         if (Verbose) {
  2580             PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
  2496           tty->print_cr("SOF Compiled Register Stack overflow at " INTPTR_FORMAT " (SIGSEGV)", pc);
  2581             address addr = (address) exceptionRecord->ExceptionInformation[1];
  2497           tty->print_cr("Register Stack access at " INTPTR_FORMAT, addr);
  2582             if (os::win32::get_frame_at_stack_banging_point(thread, exceptionInfo, pc, &fr)) {
  2498           tty->print_cr("Register Stack base " INTPTR_FORMAT, thread->register_stack_base());
  2583               assert(fr.is_java_frame(), "Must be a Java frame");
  2499           tty->print_cr("Register Stack [" INTPTR_FORMAT "," INTPTR_FORMAT "]",
  2584               SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
  2500                         thread->register_stack_base(),
  2585             }
  2501                         thread->register_stack_base() + thread->stack_size());
  2586           }
       
  2587           // Yellow zone violation.  The o/s has unprotected the first yellow
       
  2588           // zone page for us.  Note:  must call disable_stack_yellow_zone to
       
  2589           // update the enabled status, even if the zone contains only one page.
       
  2590           thread->disable_stack_yellow_reserved_zone();
       
  2591           // If not in java code, return and hope for the best.
       
  2592           return in_java
       
  2593               ? Handle_Exception(exceptionInfo, SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW))
       
  2594               :  EXCEPTION_CONTINUE_EXECUTION;
       
  2595         } else {
       
  2596           // Fatal red zone violation.
       
  2597           thread->disable_stack_red_zone();
       
  2598           tty->print_raw_cr("An unrecoverable stack overflow has occurred.");
       
  2599           report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
       
  2600                        exceptionInfo->ContextRecord);
       
  2601           return EXCEPTION_CONTINUE_SEARCH;
       
  2602         }
  2502         }
  2603       } else if (in_java) {
  2503 
  2604         // JVM-managed guard pages cannot be used on win95/98.  The o/s provides
  2504         // Reguard the permanent register stack red zone just to be sure.
  2605         // a one-time-only guard page, which it has released to us.  The next
  2505         // We saw Windows silently disabling this without telling us.
  2606         // stack overflow on this thread will result in an ACCESS_VIOLATION.
  2506         thread->enable_register_stack_red_zone();
       
  2507 
  2607         return Handle_Exception(exceptionInfo,
  2508         return Handle_Exception(exceptionInfo,
  2608                                 SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
  2509                                 SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
       
  2510       }
       
  2511 #endif
       
  2512       if (thread->stack_guards_enabled()) {
       
  2513         if (_thread_in_Java) {
       
  2514           frame fr;
       
  2515           PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
       
  2516           address addr = (address) exceptionRecord->ExceptionInformation[1];
       
  2517           if (os::win32::get_frame_at_stack_banging_point(thread, exceptionInfo, pc, &fr)) {
       
  2518             assert(fr.is_java_frame(), "Must be a Java frame");
       
  2519             SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
       
  2520           }
       
  2521         }
       
  2522         // Yellow zone violation.  The o/s has unprotected the first yellow
       
  2523         // zone page for us.  Note:  must call disable_stack_yellow_zone to
       
  2524         // update the enabled status, even if the zone contains only one page.
       
  2525         thread->disable_stack_yellow_reserved_zone();
       
  2526         // If not in java code, return and hope for the best.
       
  2527         return in_java
       
  2528             ? Handle_Exception(exceptionInfo, SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW))
       
  2529             :  EXCEPTION_CONTINUE_EXECUTION;
  2609       } else {
  2530       } else {
  2610         // Can only return and hope for the best.  Further stack growth will
  2531         // Fatal red zone violation.
  2611         // result in an ACCESS_VIOLATION.
  2532         thread->disable_stack_red_zone();
  2612         return EXCEPTION_CONTINUE_EXECUTION;
  2533         tty->print_raw_cr("An unrecoverable stack overflow has occurred.");
       
  2534         report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
       
  2535                       exceptionInfo->ContextRecord);
       
  2536         return EXCEPTION_CONTINUE_SEARCH;
  2613       }
  2537       }
  2614     } else if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
  2538     } else if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
  2615       // Either stack overflow or null pointer exception.
  2539       // Either stack overflow or null pointer exception.
  2616       if (in_java) {
  2540       if (in_java) {
  2617         PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
  2541         PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
  2677                          exceptionInfo->ContextRecord);
  2601                          exceptionInfo->ContextRecord);
  2678             return EXCEPTION_CONTINUE_SEARCH;
  2602             return EXCEPTION_CONTINUE_SEARCH;
  2679 
  2603 
  2680 #else // !IA64
  2604 #else // !IA64
  2681 
  2605 
  2682             // Windows 98 reports faulting addresses incorrectly
  2606             if (!MacroAssembler::needs_explicit_null_check((intptr_t)addr)) {
  2683             if (!MacroAssembler::needs_explicit_null_check((intptr_t)addr) ||
       
  2684                 !os::win32::is_nt()) {
       
  2685               address stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
  2607               address stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
  2686               if (stub != NULL) return Handle_Exception(exceptionInfo, stub);
  2608               if (stub != NULL) return Handle_Exception(exceptionInfo, stub);
  2687             }
  2609             }
  2688             report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
  2610             report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
  2689                          exceptionInfo->ContextRecord);
  2611                          exceptionInfo->ContextRecord);
  2868   bool build() {
  2790   bool build() {
  2869     DWORD_PTR proc_aff_mask;
  2791     DWORD_PTR proc_aff_mask;
  2870     DWORD_PTR sys_aff_mask;
  2792     DWORD_PTR sys_aff_mask;
  2871     if (!GetProcessAffinityMask(GetCurrentProcess(), &proc_aff_mask, &sys_aff_mask)) return false;
  2793     if (!GetProcessAffinityMask(GetCurrentProcess(), &proc_aff_mask, &sys_aff_mask)) return false;
  2872     ULONG highest_node_number;
  2794     ULONG highest_node_number;
  2873     if (!os::Kernel32Dll::GetNumaHighestNodeNumber(&highest_node_number)) return false;
  2795     if (!GetNumaHighestNodeNumber(&highest_node_number)) return false;
  2874     free_node_list();
  2796     free_node_list();
  2875     _numa_used_node_list = NEW_C_HEAP_ARRAY(int, highest_node_number + 1, mtInternal);
  2797     _numa_used_node_list = NEW_C_HEAP_ARRAY(int, highest_node_number + 1, mtInternal);
  2876     for (unsigned int i = 0; i <= highest_node_number; i++) {
  2798     for (unsigned int i = 0; i <= highest_node_number; i++) {
  2877       ULONGLONG proc_mask_numa_node;
  2799       ULONGLONG proc_mask_numa_node;
  2878       if (!os::Kernel32Dll::GetNumaNodeProcessorMask(i, &proc_mask_numa_node)) return false;
  2800       if (!GetNumaNodeProcessorMask(i, &proc_mask_numa_node)) return false;
  2879       if ((proc_aff_mask & proc_mask_numa_node)!=0) {
  2801       if ((proc_aff_mask & proc_mask_numa_node)!=0) {
  2880         _numa_used_node_list[_numa_used_node_count++] = i;
  2802         _numa_used_node_list[_numa_used_node_count++] = i;
  2881       }
  2803       }
  2882     }
  2804     }
  2883     return (_numa_used_node_count > 1);
  2805     return (_numa_used_node_count > 1);
  2893 
  2815 
  2894 
  2816 
  2895 
  2817 
  2896 static size_t _large_page_size = 0;
  2818 static size_t _large_page_size = 0;
  2897 
  2819 
  2898 static bool resolve_functions_for_large_page_init() {
       
  2899   return os::Kernel32Dll::GetLargePageMinimumAvailable() &&
       
  2900     os::Advapi32Dll::AdvapiAvailable();
       
  2901 }
       
  2902 
       
  2903 static bool request_lock_memory_privilege() {
  2820 static bool request_lock_memory_privilege() {
  2904   _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
  2821   _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
  2905                           os::current_process_id());
  2822                           os::current_process_id());
  2906 
  2823 
  2907   LUID luid;
  2824   LUID luid;
  2908   if (_hProcess != NULL &&
  2825   if (_hProcess != NULL &&
  2909       os::Advapi32Dll::OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
  2826       OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
  2910       os::Advapi32Dll::LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
  2827       LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
  2911 
  2828 
  2912     TOKEN_PRIVILEGES tp;
  2829     TOKEN_PRIVILEGES tp;
  2913     tp.PrivilegeCount = 1;
  2830     tp.PrivilegeCount = 1;
  2914     tp.Privileges[0].Luid = luid;
  2831     tp.Privileges[0].Luid = luid;
  2915     tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  2832     tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  2916 
  2833 
  2917     // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
  2834     // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
  2918     // privilege. Check GetLastError() too. See MSDN document.
  2835     // privilege. Check GetLastError() too. See MSDN document.
  2919     if (os::Advapi32Dll::AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
  2836     if (AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
  2920         (GetLastError() == ERROR_SUCCESS)) {
  2837         (GetLastError() == ERROR_SUCCESS)) {
  2921       return true;
  2838       return true;
  2922     }
  2839     }
  2923   }
  2840   }
  2924 
  2841 
  2942 
  2859 
  2943   // NUMAInterleaveGranularity cannot be less than vm_allocation_granularity (or _large_page_size if using large pages)
  2860   // NUMAInterleaveGranularity cannot be less than vm_allocation_granularity (or _large_page_size if using large pages)
  2944   size_t min_interleave_granularity = UseLargePages ? _large_page_size : os::vm_allocation_granularity();
  2861   size_t min_interleave_granularity = UseLargePages ? _large_page_size : os::vm_allocation_granularity();
  2945   NUMAInterleaveGranularity = align_size_up(NUMAInterleaveGranularity, min_interleave_granularity);
  2862   NUMAInterleaveGranularity = align_size_up(NUMAInterleaveGranularity, min_interleave_granularity);
  2946 
  2863 
  2947   if (os::Kernel32Dll::NumaCallsAvailable()) {
  2864   if (numa_node_list_holder.build()) {
  2948     if (numa_node_list_holder.build()) {
  2865     if (PrintMiscellaneous && Verbose) {
  2949       if (PrintMiscellaneous && Verbose) {
  2866       tty->print("NUMA UsedNodeCount=%d, namely ", numa_node_list_holder.get_count());
  2950         tty->print("NUMA UsedNodeCount=%d, namely ", numa_node_list_holder.get_count());
  2867       for (int i = 0; i < numa_node_list_holder.get_count(); i++) {
  2951         for (int i = 0; i < numa_node_list_holder.get_count(); i++) {
  2868         tty->print("%d ", numa_node_list_holder.get_node_list_entry(i));
  2952           tty->print("%d ", numa_node_list_holder.get_node_list_entry(i));
       
  2953         }
       
  2954         tty->print("\n");
       
  2955       }
  2869       }
  2956       success = true;
  2870       tty->print("\n");
  2957     } else {
  2871     }
  2958       WARN("Process does not cover multiple NUMA nodes.");
  2872     success = true;
  2959     }
       
  2960   } else {
  2873   } else {
  2961     WARN("NUMA Interleaving is not supported by the operating system.");
  2874     WARN("Process does not cover multiple NUMA nodes.");
  2962   }
  2875   }
  2963   if (!success) {
  2876   if (!success) {
  2964     if (use_numa_interleaving_specified) WARN("...Ignoring UseNUMAInterleaving flag.");
  2877     if (use_numa_interleaving_specified) WARN("...Ignoring UseNUMAInterleaving flag.");
  2965   }
  2878   }
  2966   return success;
  2879   return success;
  3043                                       prot);
  2956                                       prot);
  3044       } else {
  2957       } else {
  3045         // get the next node to use from the used_node_list
  2958         // get the next node to use from the used_node_list
  3046         assert(numa_node_list_holder.get_count() > 0, "Multiple NUMA nodes expected");
  2959         assert(numa_node_list_holder.get_count() > 0, "Multiple NUMA nodes expected");
  3047         DWORD node = numa_node_list_holder.get_node_list_entry(count % numa_node_list_holder.get_count());
  2960         DWORD node = numa_node_list_holder.get_node_list_entry(count % numa_node_list_holder.get_count());
  3048         p_new = (char *)os::Kernel32Dll::VirtualAllocExNuma(hProc,
  2961         p_new = (char *)VirtualAllocExNuma(hProc, next_alloc_addr, bytes_to_rq, flags, prot, node);
  3049                                                             next_alloc_addr,
       
  3050                                                             bytes_to_rq,
       
  3051                                                             flags,
       
  3052                                                             prot,
       
  3053                                                             node);
       
  3054       }
  2962       }
  3055     }
  2963     }
  3056 
  2964 
  3057     if (p_new == NULL) {
  2965     if (p_new == NULL) {
  3058       // Free any allocated pages
  2966       // Free any allocated pages
  3101   bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages) ||
  3009   bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages) ||
  3102                          !FLAG_IS_DEFAULT(LargePageSizeInBytes);
  3010                          !FLAG_IS_DEFAULT(LargePageSizeInBytes);
  3103   bool success = false;
  3011   bool success = false;
  3104 
  3012 
  3105 #define WARN(msg) if (warn_on_failure) { warning(msg); }
  3013 #define WARN(msg) if (warn_on_failure) { warning(msg); }
  3106   if (resolve_functions_for_large_page_init()) {
  3014   if (request_lock_memory_privilege()) {
  3107     if (request_lock_memory_privilege()) {
  3015     size_t s = GetLargePageMinimum();
  3108       size_t s = os::Kernel32Dll::GetLargePageMinimum();
  3016     if (s) {
  3109       if (s) {
       
  3110 #if defined(IA32) || defined(AMD64)
  3017 #if defined(IA32) || defined(AMD64)
  3111         if (s > 4*M || LargePageSizeInBytes > 4*M) {
  3018       if (s > 4*M || LargePageSizeInBytes > 4*M) {
  3112           WARN("JVM cannot use large pages bigger than 4mb.");
  3019         WARN("JVM cannot use large pages bigger than 4mb.");
       
  3020       } else {
       
  3021 #endif
       
  3022         if (LargePageSizeInBytes && LargePageSizeInBytes % s == 0) {
       
  3023           _large_page_size = LargePageSizeInBytes;
  3113         } else {
  3024         } else {
       
  3025           _large_page_size = s;
       
  3026         }
       
  3027         success = true;
       
  3028 #if defined(IA32) || defined(AMD64)
       
  3029       }
  3114 #endif
  3030 #endif
  3115           if (LargePageSizeInBytes && LargePageSizeInBytes % s == 0) {
       
  3116             _large_page_size = LargePageSizeInBytes;
       
  3117           } else {
       
  3118             _large_page_size = s;
       
  3119           }
       
  3120           success = true;
       
  3121 #if defined(IA32) || defined(AMD64)
       
  3122         }
       
  3123 #endif
       
  3124       } else {
       
  3125         WARN("Large page is not supported by the processor.");
       
  3126       }
       
  3127     } else {
  3031     } else {
  3128       WARN("JVM cannot use large page memory because it does not have enough privilege to lock pages in memory.");
  3032       WARN("Large page is not supported by the processor.");
  3129     }
  3033     }
  3130   } else {
  3034   } else {
  3131     WARN("Large page is not supported by the operating system.");
  3035     WARN("JVM cannot use large page memory because it does not have enough privilege to lock pages in memory.");
  3132   }
  3036   }
  3133 #undef WARN
  3037 #undef WARN
  3134 
  3038 
  3135   const size_t default_page_size = (size_t) vm_page_size();
  3039   const size_t default_page_size = (size_t) vm_page_size();
  3136   if (success && _large_page_size > default_page_size) {
  3040   if (success && _large_page_size > default_page_size) {
  3603 }
  3507 }
  3604 
  3508 
  3605 typedef BOOL (WINAPI * STTSignature)(void);
  3509 typedef BOOL (WINAPI * STTSignature)(void);
  3606 
  3510 
  3607 void os::naked_yield() {
  3511 void os::naked_yield() {
  3608   // Use either SwitchToThread() or Sleep(0)
       
  3609   // Consider passing back the return value from SwitchToThread().
  3512   // Consider passing back the return value from SwitchToThread().
  3610   if (os::Kernel32Dll::SwitchToThreadAvailable()) {
  3513   SwitchToThread();
  3611     SwitchToThread();
       
  3612   } else {
       
  3613     Sleep(0);
       
  3614   }
       
  3615 }
  3514 }
  3616 
  3515 
  3617 // Win32 only gives you access to seven real priorities at a time,
  3516 // Win32 only gives you access to seven real priorities at a time,
  3618 // so we compress Java's ten down to seven.  It would be better
  3517 // so we compress Java's ten down to seven.  It would be better
  3619 // if we dynamically adjusted relative priorities.
  3518 // if we dynamically adjusted relative priorities.
  3771 size_t os::win32::_default_stack_size        = 0;
  3670 size_t os::win32::_default_stack_size        = 0;
  3772 
  3671 
  3773 intx          os::win32::_os_thread_limit    = 0;
  3672 intx          os::win32::_os_thread_limit    = 0;
  3774 volatile intx os::win32::_os_thread_count    = 0;
  3673 volatile intx os::win32::_os_thread_count    = 0;
  3775 
  3674 
  3776 bool   os::win32::_is_nt                     = false;
       
  3777 bool   os::win32::_is_windows_2003           = false;
       
  3778 bool   os::win32::_is_windows_server         = false;
  3675 bool   os::win32::_is_windows_server         = false;
  3779 
  3676 
  3780 // 6573254
  3677 // 6573254
  3781 // Currently, the bug is observed across all the supported Windows releases,
  3678 // Currently, the bug is observed across all the supported Windows releases,
  3782 // including the latest one (as of this writing - Windows Server 2012 R2)
  3679 // including the latest one (as of this writing - Windows Server 2012 R2)
  3783 bool   os::win32::_has_exit_bug              = true;
  3680 bool   os::win32::_has_exit_bug              = true;
  3784 bool   os::win32::_has_performance_count     = 0;
       
  3785 
  3681 
  3786 void os::win32::initialize_system_info() {
  3682 void os::win32::initialize_system_info() {
  3787   SYSTEM_INFO si;
  3683   SYSTEM_INFO si;
  3788   GetSystemInfo(&si);
  3684   GetSystemInfo(&si);
  3789   _vm_page_size    = si.dwPageSize;
  3685   _vm_page_size    = si.dwPageSize;
  3802 
  3698 
  3803   OSVERSIONINFOEX oi;
  3699   OSVERSIONINFOEX oi;
  3804   oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  3700   oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  3805   GetVersionEx((OSVERSIONINFO*)&oi);
  3701   GetVersionEx((OSVERSIONINFO*)&oi);
  3806   switch (oi.dwPlatformId) {
  3702   switch (oi.dwPlatformId) {
  3807   case VER_PLATFORM_WIN32_WINDOWS: _is_nt = false; break;
       
  3808   case VER_PLATFORM_WIN32_NT:
  3703   case VER_PLATFORM_WIN32_NT:
  3809     _is_nt = true;
       
  3810     {
  3704     {
  3811       int os_vers = oi.dwMajorVersion * 1000 + oi.dwMinorVersion;
  3705       int os_vers = oi.dwMajorVersion * 1000 + oi.dwMinorVersion;
  3812       if (os_vers == 5002) {
       
  3813         _is_windows_2003 = true;
       
  3814       }
       
  3815       if (oi.wProductType == VER_NT_DOMAIN_CONTROLLER ||
  3706       if (oi.wProductType == VER_NT_DOMAIN_CONTROLLER ||
  3816           oi.wProductType == VER_NT_SERVER) {
  3707           oi.wProductType == VER_NT_SERVER) {
  3817         _is_windows_server = true;
  3708         _is_windows_server = true;
  3818       }
  3709       }
  3819     }
  3710     }
  4089   win32::initialize_system_info();
  3980   win32::initialize_system_info();
  4090   win32::setmode_streams();
  3981   win32::setmode_streams();
  4091   init_page_sizes((size_t) win32::vm_page_size());
  3982   init_page_sizes((size_t) win32::vm_page_size());
  4092 
  3983 
  4093   // This may be overridden later when argument processing is done.
  3984   // This may be overridden later when argument processing is done.
  4094   FLAG_SET_ERGO(bool, UseLargePagesIndividualAllocation,
  3985   FLAG_SET_ERGO(bool, UseLargePagesIndividualAllocation, false);
  4095                 os::win32::is_windows_2003());
       
  4096 
  3986 
  4097   // Initialize main_process and main_thread
  3987   // Initialize main_process and main_thread
  4098   main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
  3988   main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
  4099   if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
  3989   if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
  4100                        &main_thread, THREAD_ALL_ACCESS, false, 0)) {
  3990                        &main_thread, THREAD_ALL_ACCESS, false, 0)) {
  4339 }
  4229 }
  4340 
  4230 
  4341 jlong os::thread_cpu_time(Thread* thread, bool user_sys_cpu_time) {
  4231 jlong os::thread_cpu_time(Thread* thread, bool user_sys_cpu_time) {
  4342   // This code is copy from clasic VM -> hpi::sysThreadCPUTime
  4232   // This code is copy from clasic VM -> hpi::sysThreadCPUTime
  4343   // If this function changes, os::is_thread_cpu_time_supported() should too
  4233   // If this function changes, os::is_thread_cpu_time_supported() should too
  4344   if (os::win32::is_nt()) {
  4234   FILETIME CreationTime;
  4345     FILETIME CreationTime;
  4235   FILETIME ExitTime;
  4346     FILETIME ExitTime;
  4236   FILETIME KernelTime;
  4347     FILETIME KernelTime;
  4237   FILETIME UserTime;
  4348     FILETIME UserTime;
  4238 
  4349 
  4239   if (GetThreadTimes(thread->osthread()->thread_handle(), &CreationTime,
  4350     if (GetThreadTimes(thread->osthread()->thread_handle(), &CreationTime,
  4240                       &ExitTime, &KernelTime, &UserTime) == 0) {
  4351                        &ExitTime, &KernelTime, &UserTime) == 0) {
  4241     return -1;
  4352       return -1;
  4242   } else if (user_sys_cpu_time) {
  4353     } else if (user_sys_cpu_time) {
  4243     return (FT2INT64(UserTime) + FT2INT64(KernelTime)) * 100;
  4354       return (FT2INT64(UserTime) + FT2INT64(KernelTime)) * 100;
       
  4355     } else {
       
  4356       return FT2INT64(UserTime) * 100;
       
  4357     }
       
  4358   } else {
  4244   } else {
  4359     return (jlong) timeGetTime() * 1000000;
  4245     return FT2INT64(UserTime) * 100;
  4360   }
  4246   }
  4361 }
  4247 }
  4362 
  4248 
  4363 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
  4249 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
  4364   info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
  4250   info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
  4374   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
  4260   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
  4375 }
  4261 }
  4376 
  4262 
  4377 bool os::is_thread_cpu_time_supported() {
  4263 bool os::is_thread_cpu_time_supported() {
  4378   // see os::thread_cpu_time
  4264   // see os::thread_cpu_time
  4379   if (os::win32::is_nt()) {
  4265   FILETIME CreationTime;
  4380     FILETIME CreationTime;
  4266   FILETIME ExitTime;
  4381     FILETIME ExitTime;
  4267   FILETIME KernelTime;
  4382     FILETIME KernelTime;
  4268   FILETIME UserTime;
  4383     FILETIME UserTime;
  4269 
  4384 
  4270   if (GetThreadTimes(GetCurrentThread(), &CreationTime, &ExitTime,
  4385     if (GetThreadTimes(GetCurrentThread(), &CreationTime, &ExitTime,
  4271                       &KernelTime, &UserTime) == 0) {
  4386                        &KernelTime, &UserTime) == 0) {
  4272     return false;
  4387       return false;
       
  4388     } else {
       
  4389       return true;
       
  4390     }
       
  4391   } else {
  4273   } else {
  4392     return false;
  4274     return true;
  4393   }
  4275   }
  4394 }
  4276 }
  4395 
  4277 
  4396 // Windows does't provide a loadavg primitive so this is stubbed out for now.
  4278 // Windows does't provide a loadavg primitive so this is stubbed out for now.
  4397 // It does have primitives (PDH API) to get CPU usage and run queue length.
  4279 // It does have primitives (PDH API) to get CPU usage and run queue length.
  5339 bool os::is_headless_jre() { return false; }
  5221 bool os::is_headless_jre() { return false; }
  5340 
  5222 
  5341 static jint initSock() {
  5223 static jint initSock() {
  5342   WSADATA wsadata;
  5224   WSADATA wsadata;
  5343 
  5225 
  5344   if (!os::WinSock2Dll::WinSock2Available()) {
  5226   if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
  5345     jio_fprintf(stderr, "Could not load Winsock (error: %d)\n",
       
  5346                 ::GetLastError());
       
  5347     return JNI_ERR;
       
  5348   }
       
  5349 
       
  5350   if (os::WinSock2Dll::WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
       
  5351     jio_fprintf(stderr, "Could not initialize Winsock (error: %d)\n",
  5227     jio_fprintf(stderr, "Could not initialize Winsock (error: %d)\n",
  5352                 ::GetLastError());
  5228                 ::GetLastError());
  5353     return JNI_ERR;
  5229     return JNI_ERR;
  5354   }
  5230   }
  5355   return JNI_OK;
  5231   return JNI_OK;
  5356 }
  5232 }
  5357 
  5233 
  5358 struct hostent* os::get_host_by_name(char* name) {
  5234 struct hostent* os::get_host_by_name(char* name) {
  5359   return (struct hostent*)os::WinSock2Dll::gethostbyname(name);
  5235   return (struct hostent*)gethostbyname(name);
  5360 }
  5236 }
  5361 
  5237 
  5362 int os::socket_close(int fd) {
  5238 int os::socket_close(int fd) {
  5363   return ::closesocket(fd);
  5239   return ::closesocket(fd);
  5364 }
  5240 }
  5446 
  5322 
  5447   // close handle
  5323   // close handle
  5448   CloseHandle(h);
  5324   CloseHandle(h);
  5449 }
  5325 }
  5450 
  5326 
  5451 
       
  5452 // Kernel32 API
       
  5453 typedef SIZE_T (WINAPI* GetLargePageMinimum_Fn)(void);
       
  5454 typedef LPVOID (WINAPI *VirtualAllocExNuma_Fn)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
       
  5455 typedef BOOL (WINAPI *GetNumaHighestNodeNumber_Fn)(PULONG);
       
  5456 typedef BOOL (WINAPI *GetNumaNodeProcessorMask_Fn)(UCHAR, PULONGLONG);
       
  5457 typedef USHORT (WINAPI* RtlCaptureStackBackTrace_Fn)(ULONG, ULONG, PVOID*, PULONG);
       
  5458 
       
  5459 GetLargePageMinimum_Fn      os::Kernel32Dll::_GetLargePageMinimum = NULL;
       
  5460 VirtualAllocExNuma_Fn       os::Kernel32Dll::_VirtualAllocExNuma = NULL;
       
  5461 GetNumaHighestNodeNumber_Fn os::Kernel32Dll::_GetNumaHighestNodeNumber = NULL;
       
  5462 GetNumaNodeProcessorMask_Fn os::Kernel32Dll::_GetNumaNodeProcessorMask = NULL;
       
  5463 RtlCaptureStackBackTrace_Fn os::Kernel32Dll::_RtlCaptureStackBackTrace = NULL;
       
  5464 
       
  5465 
       
  5466 BOOL                        os::Kernel32Dll::initialized = FALSE;
       
  5467 SIZE_T os::Kernel32Dll::GetLargePageMinimum() {
       
  5468   assert(initialized && _GetLargePageMinimum != NULL,
       
  5469          "GetLargePageMinimumAvailable() not yet called");
       
  5470   return _GetLargePageMinimum();
       
  5471 }
       
  5472 
       
  5473 BOOL os::Kernel32Dll::GetLargePageMinimumAvailable() {
       
  5474   if (!initialized) {
       
  5475     initialize();
       
  5476   }
       
  5477   return _GetLargePageMinimum != NULL;
       
  5478 }
       
  5479 
       
  5480 BOOL os::Kernel32Dll::NumaCallsAvailable() {
       
  5481   if (!initialized) {
       
  5482     initialize();
       
  5483   }
       
  5484   return _VirtualAllocExNuma != NULL;
       
  5485 }
       
  5486 
       
  5487 LPVOID os::Kernel32Dll::VirtualAllocExNuma(HANDLE hProc, LPVOID addr,
       
  5488                                            SIZE_T bytes, DWORD flags,
       
  5489                                            DWORD prot, DWORD node) {
       
  5490   assert(initialized && _VirtualAllocExNuma != NULL,
       
  5491          "NUMACallsAvailable() not yet called");
       
  5492 
       
  5493   return _VirtualAllocExNuma(hProc, addr, bytes, flags, prot, node);
       
  5494 }
       
  5495 
       
  5496 BOOL os::Kernel32Dll::GetNumaHighestNodeNumber(PULONG ptr_highest_node_number) {
       
  5497   assert(initialized && _GetNumaHighestNodeNumber != NULL,
       
  5498          "NUMACallsAvailable() not yet called");
       
  5499 
       
  5500   return _GetNumaHighestNodeNumber(ptr_highest_node_number);
       
  5501 }
       
  5502 
       
  5503 BOOL os::Kernel32Dll::GetNumaNodeProcessorMask(UCHAR node,
       
  5504                                                PULONGLONG proc_mask) {
       
  5505   assert(initialized && _GetNumaNodeProcessorMask != NULL,
       
  5506          "NUMACallsAvailable() not yet called");
       
  5507 
       
  5508   return _GetNumaNodeProcessorMask(node, proc_mask);
       
  5509 }
       
  5510 
       
  5511 USHORT os::Kernel32Dll::RtlCaptureStackBackTrace(ULONG FrameToSkip,
       
  5512                                                  ULONG FrameToCapture,
       
  5513                                                  PVOID* BackTrace,
       
  5514                                                  PULONG BackTraceHash) {
       
  5515   if (!initialized) {
       
  5516     initialize();
       
  5517   }
       
  5518 
       
  5519   if (_RtlCaptureStackBackTrace != NULL) {
       
  5520     return _RtlCaptureStackBackTrace(FrameToSkip, FrameToCapture,
       
  5521                                      BackTrace, BackTraceHash);
       
  5522   } else {
       
  5523     return 0;
       
  5524   }
       
  5525 }
       
  5526 
       
  5527 void os::Kernel32Dll::initializeCommon() {
       
  5528   if (!initialized) {
       
  5529     HMODULE handle = ::GetModuleHandle("Kernel32.dll");
       
  5530     assert(handle != NULL, "Just check");
       
  5531     _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
       
  5532     _VirtualAllocExNuma = (VirtualAllocExNuma_Fn)::GetProcAddress(handle, "VirtualAllocExNuma");
       
  5533     _GetNumaHighestNodeNumber = (GetNumaHighestNodeNumber_Fn)::GetProcAddress(handle, "GetNumaHighestNodeNumber");
       
  5534     _GetNumaNodeProcessorMask = (GetNumaNodeProcessorMask_Fn)::GetProcAddress(handle, "GetNumaNodeProcessorMask");
       
  5535     _RtlCaptureStackBackTrace = (RtlCaptureStackBackTrace_Fn)::GetProcAddress(handle, "RtlCaptureStackBackTrace");
       
  5536     initialized = TRUE;
       
  5537   }
       
  5538 }
       
  5539 
       
  5540 bool os::start_debugging(char *buf, int buflen) {
  5327 bool os::start_debugging(char *buf, int buflen) {
  5541   int len = (int)strlen(buf);
  5328   int len = (int)strlen(buf);
  5542   char *p = &buf[len];
  5329   char *p = &buf[len];
  5543 
  5330 
  5544   jio_snprintf(p, buflen-len,
  5331   jio_snprintf(p, buflen-len,
  5559     // automatically attach to the dying VM.
  5346     // automatically attach to the dying VM.
  5560     os::breakpoint();
  5347     os::breakpoint();
  5561     yes = false;
  5348     yes = false;
  5562   }
  5349   }
  5563   return yes;
  5350   return yes;
  5564 }
       
  5565 
       
  5566 void os::Kernel32Dll::initialize() {
       
  5567   initializeCommon();
       
  5568 }
       
  5569 
       
  5570 
       
  5571 // Kernel32 API
       
  5572 inline BOOL os::Kernel32Dll::SwitchToThread() {
       
  5573   return ::SwitchToThread();
       
  5574 }
       
  5575 
       
  5576 inline BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
       
  5577   return true;
       
  5578 }
       
  5579 
       
  5580 // Help tools
       
  5581 inline BOOL os::Kernel32Dll::HelpToolsAvailable() {
       
  5582   return true;
       
  5583 }
       
  5584 
       
  5585 inline HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,
       
  5586                                                         DWORD th32ProcessId) {
       
  5587   return ::CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
       
  5588 }
       
  5589 
       
  5590 inline BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,
       
  5591                                            LPMODULEENTRY32 lpme) {
       
  5592   return ::Module32First(hSnapshot, lpme);
       
  5593 }
       
  5594 
       
  5595 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,
       
  5596                                           LPMODULEENTRY32 lpme) {
       
  5597   return ::Module32Next(hSnapshot, lpme);
       
  5598 }
       
  5599 
       
  5600 inline void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
       
  5601   ::GetNativeSystemInfo(lpSystemInfo);
       
  5602 }
       
  5603 
       
  5604 // PSAPI API
       
  5605 inline BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess,
       
  5606                                              HMODULE *lpModule, DWORD cb,
       
  5607                                              LPDWORD lpcbNeeded) {
       
  5608   return ::EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
       
  5609 }
       
  5610 
       
  5611 inline DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess,
       
  5612                                                HMODULE hModule,
       
  5613                                                LPTSTR lpFilename,
       
  5614                                                DWORD nSize) {
       
  5615   return ::GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
       
  5616 }
       
  5617 
       
  5618 inline BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess,
       
  5619                                                HMODULE hModule,
       
  5620                                                LPMODULEINFO lpmodinfo,
       
  5621                                                DWORD cb) {
       
  5622   return ::GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
       
  5623 }
       
  5624 
       
  5625 inline BOOL os::PSApiDll::PSApiAvailable() {
       
  5626   return true;
       
  5627 }
       
  5628 
       
  5629 
       
  5630 // WinSock2 API
       
  5631 inline BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested,
       
  5632                                         LPWSADATA lpWSAData) {
       
  5633   return ::WSAStartup(wVersionRequested, lpWSAData);
       
  5634 }
       
  5635 
       
  5636 inline struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
       
  5637   return ::gethostbyname(name);
       
  5638 }
       
  5639 
       
  5640 inline BOOL os::WinSock2Dll::WinSock2Available() {
       
  5641   return true;
       
  5642 }
       
  5643 
       
  5644 // Advapi API
       
  5645 inline BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
       
  5646                                                    BOOL DisableAllPrivileges,
       
  5647                                                    PTOKEN_PRIVILEGES NewState,
       
  5648                                                    DWORD BufferLength,
       
  5649                                                    PTOKEN_PRIVILEGES PreviousState,
       
  5650                                                    PDWORD ReturnLength) {
       
  5651   return ::AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
       
  5652                                  BufferLength, PreviousState, ReturnLength);
       
  5653 }
       
  5654 
       
  5655 inline BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle,
       
  5656                                               DWORD DesiredAccess,
       
  5657                                               PHANDLE TokenHandle) {
       
  5658   return ::OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
       
  5659 }
       
  5660 
       
  5661 inline BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName,
       
  5662                                                   LPCTSTR lpName,
       
  5663                                                   PLUID lpLuid) {
       
  5664   return ::LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
       
  5665 }
       
  5666 
       
  5667 inline BOOL os::Advapi32Dll::AdvapiAvailable() {
       
  5668   return true;
       
  5669 }
  5351 }
  5670 
  5352 
  5671 void* os::get_default_process_handle() {
  5353 void* os::get_default_process_handle() {
  5672   return (void*)GetModuleHandle(NULL);
  5354   return (void*)GetModuleHandle(NULL);
  5673 }
  5355 }