comparison src/os/windows/vm/os_windows.cpp @ 3464:be4ca325525a

Merge.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Jul 2011 17:32:44 -0700
parents 8b0236cbed14 7c948af3e651
children 3a05d8465ff1
comparison
equal deleted inserted replaced
3239:7c4b4daac19b 3464:be4ca325525a
96 #include <io.h> 96 #include <io.h>
97 #include <process.h> // For _beginthreadex(), _endthreadex() 97 #include <process.h> // For _beginthreadex(), _endthreadex()
98 #include <imagehlp.h> // For os::dll_address_to_function_name 98 #include <imagehlp.h> // For os::dll_address_to_function_name
99 99
100 /* for enumerating dll libraries */ 100 /* for enumerating dll libraries */
101 #include <tlhelp32.h>
102 #include <vdmdbg.h> 101 #include <vdmdbg.h>
103 102
104 // for timer info max values which include all bits 103 // for timer info max values which include all bits
105 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 104 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
106 105
239 #define PACKAGE_DIR "\\Sun\\Java" 238 #define PACKAGE_DIR "\\Sun\\Java"
240 { 239 {
241 /* Win32 library search order (See the documentation for LoadLibrary): 240 /* Win32 library search order (See the documentation for LoadLibrary):
242 * 241 *
243 * 1. The directory from which application is loaded. 242 * 1. The directory from which application is loaded.
244 * 2. The current directory 243 * 2. The system wide Java Extensions directory (Java only)
245 * 3. The system wide Java Extensions directory (Java only) 244 * 3. System directory (GetSystemDirectory)
246 * 4. System directory (GetSystemDirectory) 245 * 4. Windows directory (GetWindowsDirectory)
247 * 5. Windows directory (GetWindowsDirectory) 246 * 5. The PATH environment variable
248 * 6. The PATH environment variable 247 * 6. The current directory
249 */ 248 */
250 249
251 char *library_path; 250 char *library_path;
252 char tmp[MAX_PATH]; 251 char tmp[MAX_PATH];
253 char *path_str = ::getenv("PATH"); 252 char *path_str = ::getenv("PATH");
258 library_path[0] = '\0'; 257 library_path[0] = '\0';
259 258
260 GetModuleFileName(NULL, tmp, sizeof(tmp)); 259 GetModuleFileName(NULL, tmp, sizeof(tmp));
261 *(strrchr(tmp, '\\')) = '\0'; 260 *(strrchr(tmp, '\\')) = '\0';
262 strcat(library_path, tmp); 261 strcat(library_path, tmp);
263
264 strcat(library_path, ";.");
265 262
266 GetWindowsDirectory(tmp, sizeof(tmp)); 263 GetWindowsDirectory(tmp, sizeof(tmp));
267 strcat(library_path, ";"); 264 strcat(library_path, ";");
268 strcat(library_path, tmp); 265 strcat(library_path, tmp);
269 strcat(library_path, PACKAGE_DIR BIN_DIR); 266 strcat(library_path, PACKAGE_DIR BIN_DIR);
278 275
279 if (path_str) { 276 if (path_str) {
280 strcat(library_path, ";"); 277 strcat(library_path, ";");
281 strcat(library_path, path_str); 278 strcat(library_path, path_str);
282 } 279 }
280
281 strcat(library_path, ";.");
283 282
284 Arguments::set_library_path(library_path); 283 Arguments::set_library_path(library_path);
285 FREE_C_HEAP_ARRAY(char, library_path); 284 FREE_C_HEAP_ARRAY(char, library_path);
286 } 285 }
287 286
918 917
919 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) { 918 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
920 HINSTANCE dbghelp; 919 HINSTANCE dbghelp;
921 EXCEPTION_POINTERS ep; 920 EXCEPTION_POINTERS ep;
922 MINIDUMP_EXCEPTION_INFORMATION mei; 921 MINIDUMP_EXCEPTION_INFORMATION mei;
922 MINIDUMP_EXCEPTION_INFORMATION* pmei;
923
923 HANDLE hProcess = GetCurrentProcess(); 924 HANDLE hProcess = GetCurrentProcess();
924 DWORD processId = GetCurrentProcessId(); 925 DWORD processId = GetCurrentProcessId();
925 HANDLE dumpFile; 926 HANDLE dumpFile;
926 MINIDUMP_TYPE dumpType; 927 MINIDUMP_TYPE dumpType;
927 static const char* cwd; 928 static const char* cwd;
934 } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) { 935 } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) {
935 VMError::report_coredump_status("Minidump has been disabled from the command line", false); 936 VMError::report_coredump_status("Minidump has been disabled from the command line", false);
936 return; 937 return;
937 } 938 }
938 939
939 dbghelp = LoadLibrary("DBGHELP.DLL"); 940 dbghelp = os::win32::load_Windows_dll("DBGHELP.DLL", NULL, 0);
940 941
941 if (dbghelp == NULL) { 942 if (dbghelp == NULL) {
942 VMError::report_coredump_status("Failed to load dbghelp.dll", false); 943 VMError::report_coredump_status("Failed to load dbghelp.dll", false);
943 return; 944 return;
944 } 945 }
968 969
969 if (dumpFile == INVALID_HANDLE_VALUE) { 970 if (dumpFile == INVALID_HANDLE_VALUE) {
970 VMError::report_coredump_status("Failed to create file for dumping", false); 971 VMError::report_coredump_status("Failed to create file for dumping", false);
971 return; 972 return;
972 } 973 }
973 974 if (exceptionRecord != NULL && contextRecord != NULL) {
974 ep.ContextRecord = (PCONTEXT) contextRecord; 975 ep.ContextRecord = (PCONTEXT) contextRecord;
975 ep.ExceptionRecord = (PEXCEPTION_RECORD) exceptionRecord; 976 ep.ExceptionRecord = (PEXCEPTION_RECORD) exceptionRecord;
976 977
977 mei.ThreadId = GetCurrentThreadId(); 978 mei.ThreadId = GetCurrentThreadId();
978 mei.ExceptionPointers = &ep; 979 mei.ExceptionPointers = &ep;
980 pmei = &mei;
981 } else {
982 pmei = NULL;
983 }
984
979 985
980 // Older versions of dbghelp.dll (the one shipped with Win2003 for example) may not support all 986 // Older versions of dbghelp.dll (the one shipped with Win2003 for example) may not support all
981 // the dump types we really want. If first call fails, lets fall back to just use MiniDumpWithFullMemory then. 987 // the dump types we really want. If first call fails, lets fall back to just use MiniDumpWithFullMemory then.
982 if (_MiniDumpWriteDump(hProcess, processId, dumpFile, dumpType, &mei, NULL, NULL) == false && 988 if (_MiniDumpWriteDump(hProcess, processId, dumpFile, dumpType, pmei, NULL, NULL) == false &&
983 _MiniDumpWriteDump(hProcess, processId, dumpFile, (MINIDUMP_TYPE)MiniDumpWithFullMemory, &mei, NULL, NULL) == false) { 989 _MiniDumpWriteDump(hProcess, processId, dumpFile, (MINIDUMP_TYPE)MiniDumpWithFullMemory, pmei, NULL, NULL) == false) {
984 VMError::report_coredump_status("Call to MiniDumpWriteDump() failed", false); 990 VMError::report_coredump_status("Call to MiniDumpWriteDump() failed", false);
985 } else { 991 } else {
986 VMError::report_coredump_status(buffer, true); 992 VMError::report_coredump_status(buffer, true);
987 } 993 }
988 994
1194 return _getcwd(buf, buflen); 1200 return _getcwd(buf, buflen);
1195 } 1201 }
1196 1202
1197 //----------------------------------------------------------- 1203 //-----------------------------------------------------------
1198 // Helper functions for fatal error handler 1204 // Helper functions for fatal error handler
1199
1200 // The following library functions are resolved dynamically at runtime:
1201
1202 // PSAPI functions, for Windows NT, 2000, XP
1203
1204 // psapi.h doesn't come with Visual Studio 6; it can be downloaded as Platform
1205 // SDK from Microsoft. Here are the definitions copied from psapi.h
1206 typedef struct _MODULEINFO {
1207 LPVOID lpBaseOfDll;
1208 DWORD SizeOfImage;
1209 LPVOID EntryPoint;
1210 } MODULEINFO, *LPMODULEINFO;
1211
1212 static BOOL (WINAPI *_EnumProcessModules) ( HANDLE, HMODULE *, DWORD, LPDWORD );
1213 static DWORD (WINAPI *_GetModuleFileNameEx) ( HANDLE, HMODULE, LPTSTR, DWORD );
1214 static BOOL (WINAPI *_GetModuleInformation)( HANDLE, HMODULE, LPMODULEINFO, DWORD );
1215
1216 // ToolHelp Functions, for Windows 95, 98 and ME
1217
1218 static HANDLE(WINAPI *_CreateToolhelp32Snapshot)(DWORD,DWORD) ;
1219 static BOOL (WINAPI *_Module32First) (HANDLE,LPMODULEENTRY32) ;
1220 static BOOL (WINAPI *_Module32Next) (HANDLE,LPMODULEENTRY32) ;
1221
1222 bool _has_psapi;
1223 bool _psapi_init = false;
1224 bool _has_toolhelp;
1225
1226 static bool _init_psapi() {
1227 HINSTANCE psapi = LoadLibrary( "PSAPI.DLL" ) ;
1228 if( psapi == NULL ) return false ;
1229
1230 _EnumProcessModules = CAST_TO_FN_PTR(
1231 BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD),
1232 GetProcAddress(psapi, "EnumProcessModules")) ;
1233 _GetModuleFileNameEx = CAST_TO_FN_PTR(
1234 DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD),
1235 GetProcAddress(psapi, "GetModuleFileNameExA"));
1236 _GetModuleInformation = CAST_TO_FN_PTR(
1237 BOOL (WINAPI *)(HANDLE, HMODULE, LPMODULEINFO, DWORD),
1238 GetProcAddress(psapi, "GetModuleInformation"));
1239
1240 _has_psapi = (_EnumProcessModules && _GetModuleFileNameEx && _GetModuleInformation);
1241 _psapi_init = true;
1242 return _has_psapi;
1243 }
1244
1245 static bool _init_toolhelp() {
1246 HINSTANCE kernel32 = LoadLibrary("Kernel32.DLL") ;
1247 if (kernel32 == NULL) return false ;
1248
1249 _CreateToolhelp32Snapshot = CAST_TO_FN_PTR(
1250 HANDLE(WINAPI *)(DWORD,DWORD),
1251 GetProcAddress(kernel32, "CreateToolhelp32Snapshot"));
1252 _Module32First = CAST_TO_FN_PTR(
1253 BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1254 GetProcAddress(kernel32, "Module32First" ));
1255 _Module32Next = CAST_TO_FN_PTR(
1256 BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32),
1257 GetProcAddress(kernel32, "Module32Next" ));
1258
1259 _has_toolhelp = (_CreateToolhelp32Snapshot && _Module32First && _Module32Next);
1260 return _has_toolhelp;
1261 }
1262
1263 #ifdef _WIN64 1205 #ifdef _WIN64
1264 // Helper routine which returns true if address in 1206 // Helper routine which returns true if address in
1265 // within the NTDLL address space. 1207 // within the NTDLL address space.
1266 // 1208 //
1267 static bool _addr_in_ntdll( address addr ) 1209 static bool _addr_in_ntdll( address addr )
1269 HMODULE hmod; 1211 HMODULE hmod;
1270 MODULEINFO minfo; 1212 MODULEINFO minfo;
1271 1213
1272 hmod = GetModuleHandle("NTDLL.DLL"); 1214 hmod = GetModuleHandle("NTDLL.DLL");
1273 if ( hmod == NULL ) return false; 1215 if ( hmod == NULL ) return false;
1274 if ( !_GetModuleInformation( GetCurrentProcess(), hmod, 1216 if ( !os::PSApiDll::GetModuleInformation( GetCurrentProcess(), hmod,
1275 &minfo, sizeof(MODULEINFO)) ) 1217 &minfo, sizeof(MODULEINFO)) )
1276 return false; 1218 return false;
1277 1219
1278 if ( (addr >= minfo.lpBaseOfDll) && 1220 if ( (addr >= minfo.lpBaseOfDll) &&
1279 (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage))) 1221 (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage)))
1308 # define MAX_NUM_MODULES 128 1250 # define MAX_NUM_MODULES 128
1309 HMODULE modules[MAX_NUM_MODULES]; 1251 HMODULE modules[MAX_NUM_MODULES];
1310 static char filename[ MAX_PATH ]; 1252 static char filename[ MAX_PATH ];
1311 int result = 0; 1253 int result = 0;
1312 1254
1313 if (!_has_psapi && (_psapi_init || !_init_psapi())) return 0; 1255 if (!os::PSApiDll::PSApiAvailable()) {
1256 return 0;
1257 }
1314 1258
1315 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 1259 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
1316 FALSE, pid ) ; 1260 FALSE, pid ) ;
1317 if (hProcess == NULL) return 0; 1261 if (hProcess == NULL) return 0;
1318 1262
1319 DWORD size_needed; 1263 DWORD size_needed;
1320 if (!_EnumProcessModules(hProcess, modules, 1264 if (!os::PSApiDll::EnumProcessModules(hProcess, modules,
1321 sizeof(modules), &size_needed)) { 1265 sizeof(modules), &size_needed)) {
1322 CloseHandle( hProcess ); 1266 CloseHandle( hProcess );
1323 return 0; 1267 return 0;
1324 } 1268 }
1325 1269
1326 // number of modules that are currently loaded 1270 // number of modules that are currently loaded
1327 int num_modules = size_needed / sizeof(HMODULE); 1271 int num_modules = size_needed / sizeof(HMODULE);
1328 1272
1329 for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) { 1273 for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
1330 // Get Full pathname: 1274 // Get Full pathname:
1331 if(!_GetModuleFileNameEx(hProcess, modules[i], 1275 if(!os::PSApiDll::GetModuleFileNameEx(hProcess, modules[i],
1332 filename, sizeof(filename))) { 1276 filename, sizeof(filename))) {
1333 filename[0] = '\0'; 1277 filename[0] = '\0';
1334 } 1278 }
1335 1279
1336 MODULEINFO modinfo; 1280 MODULEINFO modinfo;
1337 if (!_GetModuleInformation(hProcess, modules[i], 1281 if (!os::PSApiDll::GetModuleInformation(hProcess, modules[i],
1338 &modinfo, sizeof(modinfo))) { 1282 &modinfo, sizeof(modinfo))) {
1339 modinfo.lpBaseOfDll = NULL; 1283 modinfo.lpBaseOfDll = NULL;
1340 modinfo.SizeOfImage = 0; 1284 modinfo.SizeOfImage = 0;
1341 } 1285 }
1342 1286
1356 { 1300 {
1357 HANDLE hSnapShot ; 1301 HANDLE hSnapShot ;
1358 static MODULEENTRY32 modentry ; 1302 static MODULEENTRY32 modentry ;
1359 int result = 0; 1303 int result = 0;
1360 1304
1361 if (!_has_toolhelp) return 0; 1305 if (!os::Kernel32Dll::HelpToolsAvailable()) {
1306 return 0;
1307 }
1362 1308
1363 // Get a handle to a Toolhelp snapshot of the system 1309 // Get a handle to a Toolhelp snapshot of the system
1364 hSnapShot = _CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ; 1310 hSnapShot = os::Kernel32Dll::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid ) ;
1365 if( hSnapShot == INVALID_HANDLE_VALUE ) { 1311 if( hSnapShot == INVALID_HANDLE_VALUE ) {
1366 return FALSE ; 1312 return FALSE ;
1367 } 1313 }
1368 1314
1369 // iterate through all modules 1315 // iterate through all modules
1370 modentry.dwSize = sizeof(MODULEENTRY32) ; 1316 modentry.dwSize = sizeof(MODULEENTRY32) ;
1371 bool not_done = _Module32First( hSnapShot, &modentry ) != 0; 1317 bool not_done = os::Kernel32Dll::Module32First( hSnapShot, &modentry ) != 0;
1372 1318
1373 while( not_done ) { 1319 while( not_done ) {
1374 // invoke the callback 1320 // invoke the callback
1375 result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr, 1321 result=func(pid, modentry.szExePath, (address)modentry.modBaseAddr,
1376 modentry.modBaseSize, param); 1322 modentry.modBaseSize, param);
1377 if (result) break; 1323 if (result) break;
1378 1324
1379 modentry.dwSize = sizeof(MODULEENTRY32) ; 1325 modentry.dwSize = sizeof(MODULEENTRY32) ;
1380 not_done = _Module32Next( hSnapShot, &modentry ) != 0; 1326 not_done = os::Kernel32Dll::Module32Next( hSnapShot, &modentry ) != 0;
1381 } 1327 }
1382 1328
1383 CloseHandle(hSnapShot); 1329 CloseHandle(hSnapShot);
1384 return result; 1330 return result;
1385 } 1331 }
1621 int pid = os::current_process_id(); 1567 int pid = os::current_process_id();
1622 st->print_cr("Dynamic libraries:"); 1568 st->print_cr("Dynamic libraries:");
1623 enumerate_modules(pid, _print_module, (void *)st); 1569 enumerate_modules(pid, _print_module, (void *)st);
1624 } 1570 }
1625 1571
1626 // function pointer to Windows API "GetNativeSystemInfo".
1627 typedef void (WINAPI *GetNativeSystemInfo_func_type)(LPSYSTEM_INFO);
1628 static GetNativeSystemInfo_func_type _GetNativeSystemInfo;
1629
1630 void os::print_os_info(outputStream* st) { 1572 void os::print_os_info(outputStream* st) {
1631 st->print("OS:"); 1573 st->print("OS:");
1632 1574
1633 OSVERSIONINFOEX osvi; 1575 OSVERSIONINFOEX osvi;
1634 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); 1576 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
1651 case 6001: { 1593 case 6001: {
1652 // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could 1594 // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could
1653 // find out whether we are running on 64 bit processor or not. 1595 // find out whether we are running on 64 bit processor or not.
1654 SYSTEM_INFO si; 1596 SYSTEM_INFO si;
1655 ZeroMemory(&si, sizeof(SYSTEM_INFO)); 1597 ZeroMemory(&si, sizeof(SYSTEM_INFO));
1656 // Check to see if _GetNativeSystemInfo has been initialized. 1598 if (!os::Kernel32Dll::GetNativeSystemInfoAvailable()){
1657 if (_GetNativeSystemInfo == NULL) {
1658 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32.dll"));
1659 _GetNativeSystemInfo =
1660 CAST_TO_FN_PTR(GetNativeSystemInfo_func_type,
1661 GetProcAddress(hKernel32,
1662 "GetNativeSystemInfo"));
1663 if (_GetNativeSystemInfo == NULL)
1664 GetSystemInfo(&si); 1599 GetSystemInfo(&si);
1665 } else { 1600 } else {
1666 _GetNativeSystemInfo(&si); 1601 os::Kernel32Dll::GetNativeSystemInfo(&si);
1667 } 1602 }
1668 if (os_vers == 5002) { 1603 if (os_vers == 5002) {
1669 if (osvi.wProductType == VER_NT_WORKSTATION && 1604 if (osvi.wProductType == VER_NT_WORKSTATION &&
1670 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) 1605 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
1671 st->print(" Windows XP x64 Edition"); 1606 st->print(" Windows XP x64 Edition");
2669 2604
2670 #ifndef MEM_LARGE_PAGES 2605 #ifndef MEM_LARGE_PAGES
2671 #define MEM_LARGE_PAGES 0x20000000 2606 #define MEM_LARGE_PAGES 0x20000000
2672 #endif 2607 #endif
2673 2608
2674 // GetLargePageMinimum is only available on Windows 2003. The other functions
2675 // are available on NT but not on Windows 98/Me. We have to resolve them at
2676 // runtime.
2677 typedef SIZE_T (WINAPI *GetLargePageMinimum_func_type) (void);
2678 typedef BOOL (WINAPI *AdjustTokenPrivileges_func_type)
2679 (HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
2680 typedef BOOL (WINAPI *OpenProcessToken_func_type) (HANDLE, DWORD, PHANDLE);
2681 typedef BOOL (WINAPI *LookupPrivilegeValue_func_type) (LPCTSTR, LPCTSTR, PLUID);
2682
2683 static GetLargePageMinimum_func_type _GetLargePageMinimum;
2684 static AdjustTokenPrivileges_func_type _AdjustTokenPrivileges;
2685 static OpenProcessToken_func_type _OpenProcessToken;
2686 static LookupPrivilegeValue_func_type _LookupPrivilegeValue;
2687
2688 static HINSTANCE _kernel32;
2689 static HINSTANCE _advapi32;
2690 static HANDLE _hProcess; 2609 static HANDLE _hProcess;
2691 static HANDLE _hToken; 2610 static HANDLE _hToken;
2692 2611
2693 static size_t _large_page_size = 0; 2612 static size_t _large_page_size = 0;
2694 2613
2695 static bool resolve_functions_for_large_page_init() { 2614 static bool resolve_functions_for_large_page_init() {
2696 _kernel32 = LoadLibrary("kernel32.dll"); 2615 return os::Kernel32Dll::GetLargePageMinimumAvailable() &&
2697 if (_kernel32 == NULL) return false; 2616 os::Advapi32Dll::AdvapiAvailable();
2698
2699 _GetLargePageMinimum = CAST_TO_FN_PTR(GetLargePageMinimum_func_type,
2700 GetProcAddress(_kernel32, "GetLargePageMinimum"));
2701 if (_GetLargePageMinimum == NULL) return false;
2702
2703 _advapi32 = LoadLibrary("advapi32.dll");
2704 if (_advapi32 == NULL) return false;
2705
2706 _AdjustTokenPrivileges = CAST_TO_FN_PTR(AdjustTokenPrivileges_func_type,
2707 GetProcAddress(_advapi32, "AdjustTokenPrivileges"));
2708 _OpenProcessToken = CAST_TO_FN_PTR(OpenProcessToken_func_type,
2709 GetProcAddress(_advapi32, "OpenProcessToken"));
2710 _LookupPrivilegeValue = CAST_TO_FN_PTR(LookupPrivilegeValue_func_type,
2711 GetProcAddress(_advapi32, "LookupPrivilegeValueA"));
2712 return _AdjustTokenPrivileges != NULL &&
2713 _OpenProcessToken != NULL &&
2714 _LookupPrivilegeValue != NULL;
2715 } 2617 }
2716 2618
2717 static bool request_lock_memory_privilege() { 2619 static bool request_lock_memory_privilege() {
2718 _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, 2620 _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
2719 os::current_process_id()); 2621 os::current_process_id());
2720 2622
2721 LUID luid; 2623 LUID luid;
2722 if (_hProcess != NULL && 2624 if (_hProcess != NULL &&
2723 _OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) && 2625 os::Advapi32Dll::OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
2724 _LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) { 2626 os::Advapi32Dll::LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
2725 2627
2726 TOKEN_PRIVILEGES tp; 2628 TOKEN_PRIVILEGES tp;
2727 tp.PrivilegeCount = 1; 2629 tp.PrivilegeCount = 1;
2728 tp.Privileges[0].Luid = luid; 2630 tp.Privileges[0].Luid = luid;
2729 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 2631 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2730 2632
2731 // AdjustTokenPrivileges() may return TRUE even when it couldn't change the 2633 // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
2732 // privilege. Check GetLastError() too. See MSDN document. 2634 // privilege. Check GetLastError() too. See MSDN document.
2733 if (_AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) && 2635 if (os::Advapi32Dll::AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
2734 (GetLastError() == ERROR_SUCCESS)) { 2636 (GetLastError() == ERROR_SUCCESS)) {
2735 return true; 2637 return true;
2736 } 2638 }
2737 } 2639 }
2738 2640
2739 return false; 2641 return false;
2740 } 2642 }
2741 2643
2742 static void cleanup_after_large_page_init() { 2644 static void cleanup_after_large_page_init() {
2743 _GetLargePageMinimum = NULL;
2744 _AdjustTokenPrivileges = NULL;
2745 _OpenProcessToken = NULL;
2746 _LookupPrivilegeValue = NULL;
2747 if (_kernel32) FreeLibrary(_kernel32);
2748 _kernel32 = NULL;
2749 if (_advapi32) FreeLibrary(_advapi32);
2750 _advapi32 = NULL;
2751 if (_hProcess) CloseHandle(_hProcess); 2645 if (_hProcess) CloseHandle(_hProcess);
2752 _hProcess = NULL; 2646 _hProcess = NULL;
2753 if (_hToken) CloseHandle(_hToken); 2647 if (_hToken) CloseHandle(_hToken);
2754 _hToken = NULL; 2648 _hToken = NULL;
2755 } 2649 }
2756 2650
2757 bool os::large_page_init() { 2651 void os::large_page_init() {
2758 if (!UseLargePages) return false; 2652 if (!UseLargePages) return;
2759 2653
2760 // print a warning if any large page related flag is specified on command line 2654 // print a warning if any large page related flag is specified on command line
2761 bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages) || 2655 bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages) ||
2762 !FLAG_IS_DEFAULT(LargePageSizeInBytes); 2656 !FLAG_IS_DEFAULT(LargePageSizeInBytes);
2763 bool success = false; 2657 bool success = false;
2764 2658
2765 # define WARN(msg) if (warn_on_failure) { warning(msg); } 2659 # define WARN(msg) if (warn_on_failure) { warning(msg); }
2766 if (resolve_functions_for_large_page_init()) { 2660 if (resolve_functions_for_large_page_init()) {
2767 if (request_lock_memory_privilege()) { 2661 if (request_lock_memory_privilege()) {
2768 size_t s = _GetLargePageMinimum(); 2662 size_t s = os::Kernel32Dll::GetLargePageMinimum();
2769 if (s) { 2663 if (s) {
2770 #if defined(IA32) || defined(AMD64) 2664 #if defined(IA32) || defined(AMD64)
2771 if (s > 4*M || LargePageSizeInBytes > 4*M) { 2665 if (s > 4*M || LargePageSizeInBytes > 4*M) {
2772 WARN("JVM cannot use large pages bigger than 4mb."); 2666 WARN("JVM cannot use large pages bigger than 4mb.");
2773 } else { 2667 } else {
2798 _page_sizes[1] = default_page_size; 2692 _page_sizes[1] = default_page_size;
2799 _page_sizes[2] = 0; 2693 _page_sizes[2] = 0;
2800 } 2694 }
2801 2695
2802 cleanup_after_large_page_init(); 2696 cleanup_after_large_page_init();
2803 return success; 2697 UseLargePages = success;
2804 } 2698 }
2805 2699
2806 // On win32, one cannot release just a part of reserved memory, it's an 2700 // On win32, one cannot release just a part of reserved memory, it's an
2807 // all or nothing deal. When we split a reservation, we must break the 2701 // all or nothing deal. When we split a reservation, we must break the
2808 // reservation into two reservations. 2702 // reservation into two reservations.
3176 typedef BOOL (WINAPI * STTSignature)(void) ; 3070 typedef BOOL (WINAPI * STTSignature)(void) ;
3177 3071
3178 os::YieldResult os::NakedYield() { 3072 os::YieldResult os::NakedYield() {
3179 // Use either SwitchToThread() or Sleep(0) 3073 // Use either SwitchToThread() or Sleep(0)
3180 // Consider passing back the return value from SwitchToThread(). 3074 // Consider passing back the return value from SwitchToThread().
3181 // We use GetProcAddress() as ancient Win9X versions of windows doen't support SwitchToThread. 3075 if (os::Kernel32Dll::SwitchToThreadAvailable()) {
3182 // In that case we revert to Sleep(0). 3076 return SwitchToThread() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
3183 static volatile STTSignature stt = (STTSignature) 1 ;
3184
3185 if (stt == ((STTSignature) 1)) {
3186 stt = (STTSignature) ::GetProcAddress (LoadLibrary ("Kernel32.dll"), "SwitchToThread") ;
3187 // It's OK if threads race during initialization as the operation above is idempotent.
3188 }
3189 if (stt != NULL) {
3190 return (*stt)() ? os::YIELD_SWITCHED : os::YIELD_NONEREADY ;
3191 } else { 3077 } else {
3192 Sleep (0) ; 3078 Sleep(0);
3193 } 3079 }
3194 return os::YIELD_UNKNOWN ; 3080 return os::YIELD_UNKNOWN ;
3195 } 3081 }
3196 3082
3197 void os::yield() { os::NakedYield(); } 3083 void os::yield() { os::NakedYield(); }
3411 StarvationMonitorInterval = 6000; 3297 StarvationMonitorInterval = 6000;
3412 } 3298 }
3413 } 3299 }
3414 3300
3415 3301
3302 HINSTANCE os::win32::load_Windows_dll(const char* name, char *ebuf, int ebuflen) {
3303 char path[MAX_PATH];
3304 DWORD size;
3305 DWORD pathLen = (DWORD)sizeof(path);
3306 HINSTANCE result = NULL;
3307
3308 // only allow library name without path component
3309 assert(strchr(name, '\\') == NULL, "path not allowed");
3310 assert(strchr(name, ':') == NULL, "path not allowed");
3311 if (strchr(name, '\\') != NULL || strchr(name, ':') != NULL) {
3312 jio_snprintf(ebuf, ebuflen,
3313 "Invalid parameter while calling os::win32::load_windows_dll(): cannot take path: %s", name);
3314 return NULL;
3315 }
3316
3317 // search system directory
3318 if ((size = GetSystemDirectory(path, pathLen)) > 0) {
3319 strcat(path, "\\");
3320 strcat(path, name);
3321 if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3322 return result;
3323 }
3324 }
3325
3326 // try Windows directory
3327 if ((size = GetWindowsDirectory(path, pathLen)) > 0) {
3328 strcat(path, "\\");
3329 strcat(path, name);
3330 if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3331 return result;
3332 }
3333 }
3334
3335 jio_snprintf(ebuf, ebuflen,
3336 "os::win32::load_windows_dll() cannot load %s from system directories.", name);
3337 return NULL;
3338 }
3339
3416 void os::win32::setmode_streams() { 3340 void os::win32::setmode_streams() {
3417 _setmode(_fileno(stdin), _O_BINARY); 3341 _setmode(_fileno(stdin), _O_BINARY);
3418 _setmode(_fileno(stdout), _O_BINARY); 3342 _setmode(_fileno(stdout), _O_BINARY);
3419 _setmode(_fileno(stderr), _O_BINARY); 3343 _setmode(_fileno(stderr), _O_BINARY);
3420 } 3344 }
3551 if(Verbose && PrintMiscellaneous) 3475 if(Verbose && PrintMiscellaneous)
3552 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page); 3476 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
3553 #endif 3477 #endif
3554 } 3478 }
3555 3479
3556 FLAG_SET_DEFAULT(UseLargePages, os::large_page_init()); 3480 os::large_page_init();
3557 3481
3558 // Setup Windows Exceptions 3482 // Setup Windows Exceptions
3559 3483
3560 // On Itanium systems, Structured Exception Handling does not 3484 // On Itanium systems, Structured Exception Handling does not
3561 // work since stack frames must be walkable by the OS. Since 3485 // work since stack frames must be walkable by the OS. Since
3643 // VM_Exit VMOperation's doit method. 3567 // VM_Exit VMOperation's doit method.
3644 if (atexit(perfMemory_exit_helper) != 0) { 3568 if (atexit(perfMemory_exit_helper) != 0) {
3645 warning("os::init_2 atexit(perfMemory_exit_helper) failed"); 3569 warning("os::init_2 atexit(perfMemory_exit_helper) failed");
3646 } 3570 }
3647 } 3571 }
3648
3649 // initialize PSAPI or ToolHelp for fatal error handler
3650 if (win32::is_nt()) _init_psapi();
3651 else _init_toolhelp();
3652 3572
3653 #ifndef _WIN64 3573 #ifndef _WIN64
3654 // Print something if NX is enabled (win32 on AMD64) 3574 // Print something if NX is enabled (win32 on AMD64)
3655 NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection()); 3575 NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection());
3656 #endif 3576 #endif
4694 4614
4695 4615
4696 // We don't build a headless jre for Windows 4616 // We don't build a headless jre for Windows
4697 bool os::is_headless_jre() { return false; } 4617 bool os::is_headless_jre() { return false; }
4698 4618
4699 // OS_SocketInterface
4700 // Not used on Windows
4701
4702 // OS_SocketInterface
4703 typedef struct hostent * (PASCAL FAR *ws2_ifn_ptr_t)(...);
4704 ws2_ifn_ptr_t *get_host_by_name_fn = NULL;
4705 4619
4706 typedef CRITICAL_SECTION mutex_t; 4620 typedef CRITICAL_SECTION mutex_t;
4707 #define mutexInit(m) InitializeCriticalSection(m) 4621 #define mutexInit(m) InitializeCriticalSection(m)
4708 #define mutexDestroy(m) DeleteCriticalSection(m) 4622 #define mutexDestroy(m) DeleteCriticalSection(m)
4709 #define mutexLock(m) EnterCriticalSection(m) 4623 #define mutexLock(m) EnterCriticalSection(m)
4710 #define mutexUnlock(m) LeaveCriticalSection(m) 4624 #define mutexUnlock(m) LeaveCriticalSection(m)
4711 4625
4712 static bool sockfnptrs_initialized = FALSE; 4626 static bool sock_initialized = FALSE;
4713 static mutex_t sockFnTableMutex; 4627 static mutex_t sockFnTableMutex;
4714 4628
4715 /* is Winsock2 loaded? better to be explicit than to rely on sockfnptrs */ 4629 static void initSock() {
4716 static bool winsock2Available = FALSE;
4717
4718
4719 static void initSockFnTable() {
4720 int (PASCAL FAR* WSAStartupPtr)(WORD, LPWSADATA);
4721 WSADATA wsadata; 4630 WSADATA wsadata;
4631
4632 if (!os::WinSock2Dll::WinSock2Available()) {
4633 jio_fprintf(stderr, "Could not load Winsock 2 (error: %d)\n",
4634 ::GetLastError());
4635 return;
4636 }
4637 if (sock_initialized == TRUE) return;
4722 4638
4723 ::mutexInit(&sockFnTableMutex); 4639 ::mutexInit(&sockFnTableMutex);
4724 ::mutexLock(&sockFnTableMutex); 4640 ::mutexLock(&sockFnTableMutex);
4725 4641 if (os::WinSock2Dll::WSAStartup(MAKEWORD(1,1), &wsadata) != 0) {
4726 if (sockfnptrs_initialized == FALSE) { 4642 jio_fprintf(stderr, "Could not initialize Winsock\n");
4727 HMODULE hWinsock; 4643 }
4728 4644 sock_initialized = TRUE;
4729 /* try to load Winsock2, and if that fails, load Winsock */
4730 hWinsock = ::LoadLibrary("ws2_32.dll");
4731
4732 if (hWinsock == NULL) {
4733 jio_fprintf(stderr, "Could not load Winsock 2 (error: %d)\n",
4734 ::GetLastError());
4735 return;
4736 }
4737
4738 /* If we loaded a DLL, then we might as well initialize it. */
4739 WSAStartupPtr = (int (PASCAL FAR *)(WORD, LPWSADATA))
4740 ::GetProcAddress(hWinsock, "WSAStartup");
4741
4742 if (WSAStartupPtr(MAKEWORD(1,1), &wsadata) != 0) {
4743 jio_fprintf(stderr, "Could not initialize Winsock\n");
4744 }
4745
4746 get_host_by_name_fn
4747 = (ws2_ifn_ptr_t*) GetProcAddress(hWinsock, "gethostbyname");
4748 }
4749
4750 assert(get_host_by_name_fn != NULL,
4751 "gethostbyname function not found");
4752 sockfnptrs_initialized = TRUE;
4753 ::mutexUnlock(&sockFnTableMutex); 4645 ::mutexUnlock(&sockFnTableMutex);
4754 } 4646 }
4755 4647
4756 struct hostent* os::get_host_by_name(char* name) { 4648 struct hostent* os::get_host_by_name(char* name) {
4757 if (!sockfnptrs_initialized) { 4649 if (!sock_initialized) {
4758 initSockFnTable(); 4650 initSock();
4759 } 4651 }
4760 4652 if (!os::WinSock2Dll::WinSock2Available()) {
4761 assert(sockfnptrs_initialized == TRUE && get_host_by_name_fn != NULL, 4653 return NULL;
4762 "sockfnptrs is not initialized or pointer to gethostbyname function is NULL"); 4654 }
4763 return (*get_host_by_name_fn)(name); 4655 return (struct hostent*)os::WinSock2Dll::gethostbyname(name);
4764 } 4656 }
4765 4657
4766 4658
4767 int os::socket_close(int fd) { 4659 int os::socket_close(int fd) {
4768 ShouldNotReachHere(); 4660 ShouldNotReachHere();
4855 int os::set_sock_opt(int fd, int level, int optname, 4747 int os::set_sock_opt(int fd, int level, int optname,
4856 const char *optval, int optlen) { 4748 const char *optval, int optlen) {
4857 ShouldNotReachHere(); 4749 ShouldNotReachHere();
4858 return 0; 4750 return 0;
4859 } 4751 }
4752
4753
4754 // Kernel32 API
4755 typedef SIZE_T (WINAPI* GetLargePageMinimum_Fn)(void);
4756 GetLargePageMinimum_Fn os::Kernel32Dll::_GetLargePageMinimum = NULL;
4757 BOOL os::Kernel32Dll::initialized = FALSE;
4758 SIZE_T os::Kernel32Dll::GetLargePageMinimum() {
4759 assert(initialized && _GetLargePageMinimum != NULL,
4760 "GetLargePageMinimumAvailable() not yet called");
4761 return _GetLargePageMinimum();
4762 }
4763
4764 BOOL os::Kernel32Dll::GetLargePageMinimumAvailable() {
4765 if (!initialized) {
4766 initialize();
4767 }
4768 return _GetLargePageMinimum != NULL;
4769 }
4770
4771
4772 #ifndef JDK6_OR_EARLIER
4773
4774 void os::Kernel32Dll::initialize() {
4775 if (!initialized) {
4776 HMODULE handle = ::GetModuleHandle("Kernel32.dll");
4777 assert(handle != NULL, "Just check");
4778 _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
4779 initialized = TRUE;
4780 }
4781 }
4782
4783
4784 // Kernel32 API
4785 inline BOOL os::Kernel32Dll::SwitchToThread() {
4786 return ::SwitchToThread();
4787 }
4788
4789 inline BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
4790 return true;
4791 }
4792
4793 // Help tools
4794 inline BOOL os::Kernel32Dll::HelpToolsAvailable() {
4795 return true;
4796 }
4797
4798 inline HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,DWORD th32ProcessId) {
4799 return ::CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
4800 }
4801
4802 inline BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4803 return ::Module32First(hSnapshot, lpme);
4804 }
4805
4806 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4807 return ::Module32Next(hSnapshot, lpme);
4808 }
4809
4810
4811 inline BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
4812 return true;
4813 }
4814
4815 inline void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
4816 ::GetNativeSystemInfo(lpSystemInfo);
4817 }
4818
4819 // PSAPI API
4820 inline BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule, DWORD cb, LPDWORD lpcbNeeded) {
4821 return ::EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
4822 }
4823
4824 inline DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize) {
4825 return ::GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
4826 }
4827
4828 inline BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb) {
4829 return ::GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
4830 }
4831
4832 inline BOOL os::PSApiDll::PSApiAvailable() {
4833 return true;
4834 }
4835
4836
4837 // WinSock2 API
4838 inline BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
4839 return ::WSAStartup(wVersionRequested, lpWSAData);
4840 }
4841
4842 inline struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
4843 return ::gethostbyname(name);
4844 }
4845
4846 inline BOOL os::WinSock2Dll::WinSock2Available() {
4847 return true;
4848 }
4849
4850 // Advapi API
4851 inline BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
4852 BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength,
4853 PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength) {
4854 return ::AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
4855 BufferLength, PreviousState, ReturnLength);
4856 }
4857
4858 inline BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess,
4859 PHANDLE TokenHandle) {
4860 return ::OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
4861 }
4862
4863 inline BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid) {
4864 return ::LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
4865 }
4866
4867 inline BOOL os::Advapi32Dll::AdvapiAvailable() {
4868 return true;
4869 }
4870
4871 #else
4872 // Kernel32 API
4873 typedef BOOL (WINAPI* SwitchToThread_Fn)(void);
4874 typedef HANDLE (WINAPI* CreateToolhelp32Snapshot_Fn)(DWORD,DWORD);
4875 typedef BOOL (WINAPI* Module32First_Fn)(HANDLE,LPMODULEENTRY32);
4876 typedef BOOL (WINAPI* Module32Next_Fn)(HANDLE,LPMODULEENTRY32);
4877 typedef void (WINAPI* GetNativeSystemInfo_Fn)(LPSYSTEM_INFO);
4878
4879 SwitchToThread_Fn os::Kernel32Dll::_SwitchToThread = NULL;
4880 CreateToolhelp32Snapshot_Fn os::Kernel32Dll::_CreateToolhelp32Snapshot = NULL;
4881 Module32First_Fn os::Kernel32Dll::_Module32First = NULL;
4882 Module32Next_Fn os::Kernel32Dll::_Module32Next = NULL;
4883 GetNativeSystemInfo_Fn os::Kernel32Dll::_GetNativeSystemInfo = NULL;
4884
4885 void os::Kernel32Dll::initialize() {
4886 if (!initialized) {
4887 HMODULE handle = ::GetModuleHandle("Kernel32.dll");
4888 assert(handle != NULL, "Just check");
4889
4890 _SwitchToThread = (SwitchToThread_Fn)::GetProcAddress(handle, "SwitchToThread");
4891 _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
4892 _CreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_Fn)
4893 ::GetProcAddress(handle, "CreateToolhelp32Snapshot");
4894 _Module32First = (Module32First_Fn)::GetProcAddress(handle, "Module32First");
4895 _Module32Next = (Module32Next_Fn)::GetProcAddress(handle, "Module32Next");
4896 _GetNativeSystemInfo = (GetNativeSystemInfo_Fn)::GetProcAddress(handle, "GetNativeSystemInfo");
4897
4898 initialized = TRUE;
4899 }
4900 }
4901
4902 BOOL os::Kernel32Dll::SwitchToThread() {
4903 assert(initialized && _SwitchToThread != NULL,
4904 "SwitchToThreadAvailable() not yet called");
4905 return _SwitchToThread();
4906 }
4907
4908
4909 BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
4910 if (!initialized) {
4911 initialize();
4912 }
4913 return _SwitchToThread != NULL;
4914 }
4915
4916 // Help tools
4917 BOOL os::Kernel32Dll::HelpToolsAvailable() {
4918 if (!initialized) {
4919 initialize();
4920 }
4921 return _CreateToolhelp32Snapshot != NULL &&
4922 _Module32First != NULL &&
4923 _Module32Next != NULL;
4924 }
4925
4926 HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,DWORD th32ProcessId) {
4927 assert(initialized && _CreateToolhelp32Snapshot != NULL,
4928 "HelpToolsAvailable() not yet called");
4929
4930 return _CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
4931 }
4932
4933 BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4934 assert(initialized && _Module32First != NULL,
4935 "HelpToolsAvailable() not yet called");
4936
4937 return _Module32First(hSnapshot, lpme);
4938 }
4939
4940 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
4941 assert(initialized && _Module32Next != NULL,
4942 "HelpToolsAvailable() not yet called");
4943
4944 return _Module32Next(hSnapshot, lpme);
4945 }
4946
4947
4948 BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
4949 if (!initialized) {
4950 initialize();
4951 }
4952 return _GetNativeSystemInfo != NULL;
4953 }
4954
4955 void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
4956 assert(initialized && _GetNativeSystemInfo != NULL,
4957 "GetNativeSystemInfoAvailable() not yet called");
4958
4959 _GetNativeSystemInfo(lpSystemInfo);
4960 }
4961
4962 // PSAPI API
4963
4964
4965 typedef BOOL (WINAPI *EnumProcessModules_Fn)(HANDLE, HMODULE *, DWORD, LPDWORD);
4966 typedef BOOL (WINAPI *GetModuleFileNameEx_Fn)(HANDLE, HMODULE, LPTSTR, DWORD);;
4967 typedef BOOL (WINAPI *GetModuleInformation_Fn)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
4968
4969 EnumProcessModules_Fn os::PSApiDll::_EnumProcessModules = NULL;
4970 GetModuleFileNameEx_Fn os::PSApiDll::_GetModuleFileNameEx = NULL;
4971 GetModuleInformation_Fn os::PSApiDll::_GetModuleInformation = NULL;
4972 BOOL os::PSApiDll::initialized = FALSE;
4973
4974 void os::PSApiDll::initialize() {
4975 if (!initialized) {
4976 HMODULE handle = os::win32::load_Windows_dll("PSAPI.DLL", NULL, 0);
4977 if (handle != NULL) {
4978 _EnumProcessModules = (EnumProcessModules_Fn)::GetProcAddress(handle,
4979 "EnumProcessModules");
4980 _GetModuleFileNameEx = (GetModuleFileNameEx_Fn)::GetProcAddress(handle,
4981 "GetModuleFileNameExA");
4982 _GetModuleInformation = (GetModuleInformation_Fn)::GetProcAddress(handle,
4983 "GetModuleInformation");
4984 }
4985 initialized = TRUE;
4986 }
4987 }
4988
4989
4990
4991 BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule, DWORD cb, LPDWORD lpcbNeeded) {
4992 assert(initialized && _EnumProcessModules != NULL,
4993 "PSApiAvailable() not yet called");
4994 return _EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
4995 }
4996
4997 DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize) {
4998 assert(initialized && _GetModuleFileNameEx != NULL,
4999 "PSApiAvailable() not yet called");
5000 return _GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
5001 }
5002
5003 BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb) {
5004 assert(initialized && _GetModuleInformation != NULL,
5005 "PSApiAvailable() not yet called");
5006 return _GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5007 }
5008
5009 BOOL os::PSApiDll::PSApiAvailable() {
5010 if (!initialized) {
5011 initialize();
5012 }
5013 return _EnumProcessModules != NULL &&
5014 _GetModuleFileNameEx != NULL &&
5015 _GetModuleInformation != NULL;
5016 }
5017
5018
5019 // WinSock2 API
5020 typedef int (PASCAL FAR* WSAStartup_Fn)(WORD, LPWSADATA);
5021 typedef struct hostent *(PASCAL FAR *gethostbyname_Fn)(...);
5022
5023 WSAStartup_Fn os::WinSock2Dll::_WSAStartup = NULL;
5024 gethostbyname_Fn os::WinSock2Dll::_gethostbyname = NULL;
5025 BOOL os::WinSock2Dll::initialized = FALSE;
5026
5027 void os::WinSock2Dll::initialize() {
5028 if (!initialized) {
5029 HMODULE handle = os::win32::load_Windows_dll("ws2_32.dll", NULL, 0);
5030 if (handle != NULL) {
5031 _WSAStartup = (WSAStartup_Fn)::GetProcAddress(handle, "WSAStartup");
5032 _gethostbyname = (gethostbyname_Fn)::GetProcAddress(handle, "gethostbyname");
5033 }
5034 initialized = TRUE;
5035 }
5036 }
5037
5038
5039 BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
5040 assert(initialized && _WSAStartup != NULL,
5041 "WinSock2Available() not yet called");
5042 return _WSAStartup(wVersionRequested, lpWSAData);
5043 }
5044
5045 struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5046 assert(initialized && _gethostbyname != NULL,
5047 "WinSock2Available() not yet called");
5048 return _gethostbyname(name);
5049 }
5050
5051 BOOL os::WinSock2Dll::WinSock2Available() {
5052 if (!initialized) {
5053 initialize();
5054 }
5055 return _WSAStartup != NULL &&
5056 _gethostbyname != NULL;
5057 }
5058
5059 typedef BOOL (WINAPI *AdjustTokenPrivileges_Fn)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
5060 typedef BOOL (WINAPI *OpenProcessToken_Fn)(HANDLE, DWORD, PHANDLE);
5061 typedef BOOL (WINAPI *LookupPrivilegeValue_Fn)(LPCTSTR, LPCTSTR, PLUID);
5062
5063 AdjustTokenPrivileges_Fn os::Advapi32Dll::_AdjustTokenPrivileges = NULL;
5064 OpenProcessToken_Fn os::Advapi32Dll::_OpenProcessToken = NULL;
5065 LookupPrivilegeValue_Fn os::Advapi32Dll::_LookupPrivilegeValue = NULL;
5066 BOOL os::Advapi32Dll::initialized = FALSE;
5067
5068 void os::Advapi32Dll::initialize() {
5069 if (!initialized) {
5070 HMODULE handle = os::win32::load_Windows_dll("advapi32.dll", NULL, 0);
5071 if (handle != NULL) {
5072 _AdjustTokenPrivileges = (AdjustTokenPrivileges_Fn)::GetProcAddress(handle,
5073 "AdjustTokenPrivileges");
5074 _OpenProcessToken = (OpenProcessToken_Fn)::GetProcAddress(handle,
5075 "OpenProcessToken");
5076 _LookupPrivilegeValue = (LookupPrivilegeValue_Fn)::GetProcAddress(handle,
5077 "LookupPrivilegeValue");
5078 }
5079 initialized = TRUE;
5080 }
5081 }
5082
5083 BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5084 BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength,
5085 PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength) {
5086 assert(initialized && _AdjustTokenPrivileges != NULL,
5087 "AdvapiAvailable() not yet called");
5088 return _AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5089 BufferLength, PreviousState, ReturnLength);
5090 }
5091
5092 BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess,
5093 PHANDLE TokenHandle) {
5094 assert(initialized && _OpenProcessToken != NULL,
5095 "AdvapiAvailable() not yet called");
5096 return _OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5097 }
5098
5099 BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid) {
5100 assert(initialized && _LookupPrivilegeValue != NULL,
5101 "AdvapiAvailable() not yet called");
5102 return _LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5103 }
5104
5105 BOOL os::Advapi32Dll::AdvapiAvailable() {
5106 if (!initialized) {
5107 initialize();
5108 }
5109 return _AdjustTokenPrivileges != NULL &&
5110 _OpenProcessToken != NULL &&
5111 _LookupPrivilegeValue != NULL;
5112 }
5113
5114 #endif
5115