< prev index next >

src/os/windows/vm/os_windows.cpp

Print this page




 402   if (time_struct_ptr != NULL) {
 403     *res = *time_struct_ptr;
 404     return res;
 405   }
 406   return NULL;
 407 }
 408 
 409 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo);
 410 
 411 // Thread start routine for all new Java threads
 412 static unsigned __stdcall java_start(Thread* thread) {
 413   // Try to randomize the cache line index of hot stack frames.
 414   // This helps when threads of the same stack traces evict each other's
 415   // cache lines. The threads can be either from the same JVM instance, or
 416   // from different JVM instances. The benefit is especially true for
 417   // processors with hyperthreading technology.
 418   static int counter = 0;
 419   int pid = os::current_process_id();
 420   _alloca(((pid ^ counter++) & 7) * 128);
 421 


 422   OSThread* osthr = thread->osthread();
 423   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 424 
 425   if (UseNUMA) {
 426     int lgrp_id = os::numa_get_group_id();
 427     if (lgrp_id != -1) {
 428       thread->set_lgrp_id(lgrp_id);
 429     }
 430   }
 431 
 432   // Diagnostic code to investigate JDK-6573254
 433   int res = 30115;  // non-java thread
 434   if (thread->is_Java_thread()) {
 435     res = 20115;    // java thread
 436   }
 437 
 438   // Install a win32 structured exception handler around every thread created
 439   // by VM, so VM can generate error dump when an exception occurred in non-
 440   // Java thread (e.g. VM thread).
 441   __try {


2358 //-----------------------------------------------------------------------------
2359 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
2360   if (InterceptOSException) return EXCEPTION_CONTINUE_SEARCH;
2361   DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
2362 #ifdef _M_IA64
2363   // On Itanium, we need the "precise pc", which has the slot number coded
2364   // into the least 4 bits: 0000=slot0, 0100=slot1, 1000=slot2 (Windows format).
2365   address pc = (address) exceptionInfo->ExceptionRecord->ExceptionAddress;
2366   // Convert the pc to "Unix format", which has the slot number coded
2367   // into the least 2 bits: 0000=slot0, 0001=slot1, 0010=slot2
2368   // This is needed for IA64 because "relocation" / "implicit null check" / "poll instruction"
2369   // information is saved in the Unix format.
2370   address pc_unix_format = (address) ((((uint64_t)pc) & 0xFFFFFFFFFFFFFFF0) | ((((uint64_t)pc) & 0xF) >> 2));
2371 #else
2372   #ifdef _M_AMD64
2373   address pc = (address) exceptionInfo->ContextRecord->Rip;
2374   #else
2375   address pc = (address) exceptionInfo->ContextRecord->Eip;
2376   #endif
2377 #endif
2378   Thread* t = ThreadLocalStorage::get_thread_slow();          // slow & steady
2379 
2380   // Handle SafeFetch32 and SafeFetchN exceptions.
2381   if (StubRoutines::is_safefetch_fault(pc)) {
2382     return Handle_Exception(exceptionInfo, StubRoutines::continuation_for_safefetch_fault(pc));
2383   }
2384 
2385 #ifndef _WIN64
2386   // Execution protection violation - win32 running on AMD64 only
2387   // Handled first to avoid misdiagnosis as a "normal" access violation;
2388   // This is safe to do because we have a new/unique ExceptionInformation
2389   // code for this condition.
2390   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2391     PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2392     int exception_subcode = (int) exceptionRecord->ExceptionInformation[0];
2393     address addr = (address) exceptionRecord->ExceptionInformation[1];
2394 
2395     if (exception_subcode == EXCEPTION_INFO_EXEC_VIOLATION) {
2396       int page_size = os::vm_page_size();
2397 
2398       // Make sure the pc and the faulting address are sane.


3985 
3986 bool os::is_debugger_attached() {
3987   return IsDebuggerPresent() ? true : false;
3988 }
3989 
3990 
3991 void os::wait_for_keypress_at_exit(void) {
3992   if (PauseAtExit) {
3993     fprintf(stderr, "Press any key to continue...\n");
3994     fgetc(stdin);
3995   }
3996 }
3997 
3998 
3999 int os::message_box(const char* title, const char* message) {
4000   int result = MessageBox(NULL, message, title,
4001                           MB_YESNO | MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFAULT_DESKTOP_ONLY);
4002   return result == IDYES;
4003 }
4004 
4005 int os::allocate_thread_local_storage() {
4006   return TlsAlloc();
4007 }
4008 
4009 
4010 void os::free_thread_local_storage(int index) {
4011   TlsFree(index);
4012 }
4013 
4014 
4015 void os::thread_local_storage_at_put(int index, void* value) {
4016   TlsSetValue(index, value);
4017   assert(thread_local_storage_at(index) == value, "Just checking");
4018 }
4019 
4020 
4021 void* os::thread_local_storage_at(int index) {
4022   return TlsGetValue(index);
4023 }
4024 
4025 
4026 #ifndef PRODUCT
4027 #ifndef _WIN64
4028 // Helpers to check whether NX protection is enabled
4029 int nx_exception_filter(_EXCEPTION_POINTERS *pex) {
4030   if (pex->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
4031       pex->ExceptionRecord->NumberParameters > 0 &&
4032       pex->ExceptionRecord->ExceptionInformation[0] ==
4033       EXCEPTION_INFO_EXEC_VIOLATION) {
4034     return EXCEPTION_EXECUTE_HANDLER;
4035   }
4036   return EXCEPTION_CONTINUE_SEARCH;
4037 }
4038 
4039 void nx_check_protection() {
4040   // If NX is enabled we'll get an exception calling into code on the stack
4041   char code[] = { (char)0xC3 }; // ret
4042   void *code_ptr = (void *)code;
4043   __try {
4044     __asm call code_ptr
4045   } __except(nx_exception_filter((_EXCEPTION_POINTERS*)_exception_info())) {


4053 void os::init(void) {
4054   _initial_pid = _getpid();
4055 
4056   init_random(1234567);
4057 
4058   win32::initialize_system_info();
4059   win32::setmode_streams();
4060   init_page_sizes((size_t) win32::vm_page_size());
4061 
4062   // This may be overridden later when argument processing is done.
4063   FLAG_SET_ERGO(bool, UseLargePagesIndividualAllocation,
4064                 os::win32::is_windows_2003());
4065 
4066   // Initialize main_process and main_thread
4067   main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
4068   if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
4069                        &main_thread, THREAD_ALL_ACCESS, false, 0)) {
4070     fatal("DuplicateHandle failed\n");
4071   }
4072   main_thread_id = (int) GetCurrentThreadId();



4073 }
4074 
4075 // To install functions for atexit processing
4076 extern "C" {
4077   static void perfMemory_exit_helper() {
4078     perfMemory_exit();
4079   }
4080 }
4081 
4082 static jint initSock();
4083 
4084 // this is called _after_ the global arguments have been parsed
4085 jint os::init_2(void) {
4086   // Allocate a single page and mark it as readable for safepoint polling
4087   address polling_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READONLY);
4088   guarantee(polling_page != NULL, "Reserve Failed for polling page");
4089 
4090   address return_page  = (address)VirtualAlloc(polling_page, os::vm_page_size(), MEM_COMMIT, PAGE_READONLY);
4091   guarantee(return_page != NULL, "Commit Failed for polling page");
4092 


5265       if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) {
5266         fatal("heap walk aborted with error %d", err);
5267       }
5268       HeapUnlock(heap);
5269     }
5270     mallocDebugIntervalCounter = 0;
5271   }
5272   return true;
5273 }
5274 
5275 
5276 bool os::find(address addr, outputStream* st) {
5277   // Nothing yet
5278   return false;
5279 }
5280 
5281 LONG WINAPI os::win32::serialize_fault_filter(struct _EXCEPTION_POINTERS* e) {
5282   DWORD exception_code = e->ExceptionRecord->ExceptionCode;
5283 
5284   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
5285     JavaThread* thread = (JavaThread*)ThreadLocalStorage::get_thread_slow();
5286     PEXCEPTION_RECORD exceptionRecord = e->ExceptionRecord;
5287     address addr = (address) exceptionRecord->ExceptionInformation[1];
5288 
5289     if (os::is_memory_serialize_page(thread, addr)) {
5290       return EXCEPTION_CONTINUE_EXECUTION;
5291     }
5292   }
5293 
5294   return EXCEPTION_CONTINUE_SEARCH;
5295 }
5296 
5297 // We don't build a headless jre for Windows
5298 bool os::is_headless_jre() { return false; }
5299 
5300 static jint initSock() {
5301   WSADATA wsadata;
5302 
5303   if (!os::WinSock2Dll::WinSock2Available()) {
5304     jio_fprintf(stderr, "Could not load Winsock (error: %d)\n",
5305                 ::GetLastError());


5975     if (actual_location == NULL) {
5976       if (VerboseInternalVMTests) {
5977         gclog_or_tty->print("Failed to allocate any memory at " PTR_FORMAT " size " SIZE_FORMAT ". Skipping remainder of test.",
5978                             expected_location, large_allocation_size);
5979       }
5980     } else {
5981       // release memory
5982       os::release_memory_special(actual_location, expected_allocation_size);
5983       // only now check, after releasing any memory to avoid any leaks.
5984       assert(actual_location == expected_location,
5985              "Failed to allocate memory at requested location " PTR_FORMAT " of size " SIZE_FORMAT ", is " PTR_FORMAT " instead",
5986              expected_location, expected_allocation_size, actual_location);
5987     }
5988   }
5989 
5990   // restore globals
5991   UseLargePagesIndividualAllocation = old_use_large_pages_individual_allocation;
5992   UseNUMAInterleaving = old_use_numa_interleaving;
5993 }
5994 #endif // PRODUCT















 402   if (time_struct_ptr != NULL) {
 403     *res = *time_struct_ptr;
 404     return res;
 405   }
 406   return NULL;
 407 }
 408 
 409 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo);
 410 
 411 // Thread start routine for all new Java threads
 412 static unsigned __stdcall java_start(Thread* thread) {
 413   // Try to randomize the cache line index of hot stack frames.
 414   // This helps when threads of the same stack traces evict each other's
 415   // cache lines. The threads can be either from the same JVM instance, or
 416   // from different JVM instances. The benefit is especially true for
 417   // processors with hyperthreading technology.
 418   static int counter = 0;
 419   int pid = os::current_process_id();
 420   _alloca(((pid ^ counter++) & 7) * 128);
 421 
 422   thread->initialize_thread_current();
 423 
 424   OSThread* osthr = thread->osthread();
 425   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 426 
 427   if (UseNUMA) {
 428     int lgrp_id = os::numa_get_group_id();
 429     if (lgrp_id != -1) {
 430       thread->set_lgrp_id(lgrp_id);
 431     }
 432   }
 433 
 434   // Diagnostic code to investigate JDK-6573254
 435   int res = 30115;  // non-java thread
 436   if (thread->is_Java_thread()) {
 437     res = 20115;    // java thread
 438   }
 439 
 440   // Install a win32 structured exception handler around every thread created
 441   // by VM, so VM can generate error dump when an exception occurred in non-
 442   // Java thread (e.g. VM thread).
 443   __try {


2360 //-----------------------------------------------------------------------------
2361 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
2362   if (InterceptOSException) return EXCEPTION_CONTINUE_SEARCH;
2363   DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
2364 #ifdef _M_IA64
2365   // On Itanium, we need the "precise pc", which has the slot number coded
2366   // into the least 4 bits: 0000=slot0, 0100=slot1, 1000=slot2 (Windows format).
2367   address pc = (address) exceptionInfo->ExceptionRecord->ExceptionAddress;
2368   // Convert the pc to "Unix format", which has the slot number coded
2369   // into the least 2 bits: 0000=slot0, 0001=slot1, 0010=slot2
2370   // This is needed for IA64 because "relocation" / "implicit null check" / "poll instruction"
2371   // information is saved in the Unix format.
2372   address pc_unix_format = (address) ((((uint64_t)pc) & 0xFFFFFFFFFFFFFFF0) | ((((uint64_t)pc) & 0xF) >> 2));
2373 #else
2374   #ifdef _M_AMD64
2375   address pc = (address) exceptionInfo->ContextRecord->Rip;
2376   #else
2377   address pc = (address) exceptionInfo->ContextRecord->Eip;
2378   #endif
2379 #endif
2380   Thread* t = Thread::current();
2381 
2382   // Handle SafeFetch32 and SafeFetchN exceptions.
2383   if (StubRoutines::is_safefetch_fault(pc)) {
2384     return Handle_Exception(exceptionInfo, StubRoutines::continuation_for_safefetch_fault(pc));
2385   }
2386 
2387 #ifndef _WIN64
2388   // Execution protection violation - win32 running on AMD64 only
2389   // Handled first to avoid misdiagnosis as a "normal" access violation;
2390   // This is safe to do because we have a new/unique ExceptionInformation
2391   // code for this condition.
2392   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2393     PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2394     int exception_subcode = (int) exceptionRecord->ExceptionInformation[0];
2395     address addr = (address) exceptionRecord->ExceptionInformation[1];
2396 
2397     if (exception_subcode == EXCEPTION_INFO_EXEC_VIOLATION) {
2398       int page_size = os::vm_page_size();
2399 
2400       // Make sure the pc and the faulting address are sane.


3987 
3988 bool os::is_debugger_attached() {
3989   return IsDebuggerPresent() ? true : false;
3990 }
3991 
3992 
3993 void os::wait_for_keypress_at_exit(void) {
3994   if (PauseAtExit) {
3995     fprintf(stderr, "Press any key to continue...\n");
3996     fgetc(stdin);
3997   }
3998 }
3999 
4000 
4001 int os::message_box(const char* title, const char* message) {
4002   int result = MessageBox(NULL, message, title,
4003                           MB_YESNO | MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFAULT_DESKTOP_ONLY);
4004   return result == IDYES;
4005 }
4006 





















4007 #ifndef PRODUCT
4008 #ifndef _WIN64
4009 // Helpers to check whether NX protection is enabled
4010 int nx_exception_filter(_EXCEPTION_POINTERS *pex) {
4011   if (pex->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
4012       pex->ExceptionRecord->NumberParameters > 0 &&
4013       pex->ExceptionRecord->ExceptionInformation[0] ==
4014       EXCEPTION_INFO_EXEC_VIOLATION) {
4015     return EXCEPTION_EXECUTE_HANDLER;
4016   }
4017   return EXCEPTION_CONTINUE_SEARCH;
4018 }
4019 
4020 void nx_check_protection() {
4021   // If NX is enabled we'll get an exception calling into code on the stack
4022   char code[] = { (char)0xC3 }; // ret
4023   void *code_ptr = (void *)code;
4024   __try {
4025     __asm call code_ptr
4026   } __except(nx_exception_filter((_EXCEPTION_POINTERS*)_exception_info())) {


4034 void os::init(void) {
4035   _initial_pid = _getpid();
4036 
4037   init_random(1234567);
4038 
4039   win32::initialize_system_info();
4040   win32::setmode_streams();
4041   init_page_sizes((size_t) win32::vm_page_size());
4042 
4043   // This may be overridden later when argument processing is done.
4044   FLAG_SET_ERGO(bool, UseLargePagesIndividualAllocation,
4045                 os::win32::is_windows_2003());
4046 
4047   // Initialize main_process and main_thread
4048   main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
4049   if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
4050                        &main_thread, THREAD_ALL_ACCESS, false, 0)) {
4051     fatal("DuplicateHandle failed\n");
4052   }
4053   main_thread_id = (int) GetCurrentThreadId();
4054 
4055   // initialize fast thread access - only used for 32-bit
4056   win32::initialize_thread_ptr_offset();
4057 }
4058 
4059 // To install functions for atexit processing
4060 extern "C" {
4061   static void perfMemory_exit_helper() {
4062     perfMemory_exit();
4063   }
4064 }
4065 
4066 static jint initSock();
4067 
4068 // this is called _after_ the global arguments have been parsed
4069 jint os::init_2(void) {
4070   // Allocate a single page and mark it as readable for safepoint polling
4071   address polling_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READONLY);
4072   guarantee(polling_page != NULL, "Reserve Failed for polling page");
4073 
4074   address return_page  = (address)VirtualAlloc(polling_page, os::vm_page_size(), MEM_COMMIT, PAGE_READONLY);
4075   guarantee(return_page != NULL, "Commit Failed for polling page");
4076 


5249       if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) {
5250         fatal("heap walk aborted with error %d", err);
5251       }
5252       HeapUnlock(heap);
5253     }
5254     mallocDebugIntervalCounter = 0;
5255   }
5256   return true;
5257 }
5258 
5259 
5260 bool os::find(address addr, outputStream* st) {
5261   // Nothing yet
5262   return false;
5263 }
5264 
5265 LONG WINAPI os::win32::serialize_fault_filter(struct _EXCEPTION_POINTERS* e) {
5266   DWORD exception_code = e->ExceptionRecord->ExceptionCode;
5267 
5268   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
5269     JavaThread* thread = JavaThread::current();
5270     PEXCEPTION_RECORD exceptionRecord = e->ExceptionRecord;
5271     address addr = (address) exceptionRecord->ExceptionInformation[1];
5272 
5273     if (os::is_memory_serialize_page(thread, addr)) {
5274       return EXCEPTION_CONTINUE_EXECUTION;
5275     }
5276   }
5277 
5278   return EXCEPTION_CONTINUE_SEARCH;
5279 }
5280 
5281 // We don't build a headless jre for Windows
5282 bool os::is_headless_jre() { return false; }
5283 
5284 static jint initSock() {
5285   WSADATA wsadata;
5286 
5287   if (!os::WinSock2Dll::WinSock2Available()) {
5288     jio_fprintf(stderr, "Could not load Winsock (error: %d)\n",
5289                 ::GetLastError());


5959     if (actual_location == NULL) {
5960       if (VerboseInternalVMTests) {
5961         gclog_or_tty->print("Failed to allocate any memory at " PTR_FORMAT " size " SIZE_FORMAT ". Skipping remainder of test.",
5962                             expected_location, large_allocation_size);
5963       }
5964     } else {
5965       // release memory
5966       os::release_memory_special(actual_location, expected_allocation_size);
5967       // only now check, after releasing any memory to avoid any leaks.
5968       assert(actual_location == expected_location,
5969              "Failed to allocate memory at requested location " PTR_FORMAT " of size " SIZE_FORMAT ", is " PTR_FORMAT " instead",
5970              expected_location, expected_allocation_size, actual_location);
5971     }
5972   }
5973 
5974   // restore globals
5975   UseLargePagesIndividualAllocation = old_use_large_pages_individual_allocation;
5976   UseNUMAInterleaving = old_use_numa_interleaving;
5977 }
5978 #endif // PRODUCT
5979 
5980 // Fast current thread access
5981 
5982 int os::win32::_thread_ptr_offset = 0;
5983 
5984 static void call_wrapper_dummy() {}
5985 
5986 // We need to call the os_exception_wrapper once so that it sets
5987 // up the offset from FS of the thread pointer.
5988 void os::win32::initialize_thread_ptr_offset() {
5989       os::os_exception_wrapper( (java_call_t)call_wrapper_dummy,
5990                                 NULL, NULL, NULL, NULL);
5991 }
< prev index next >