1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef OS_WINDOWS_VM_OS_WINDOWS_HPP
  26 #define OS_WINDOWS_VM_OS_WINDOWS_HPP
  27 // Win32_OS defines the interface to windows operating systems
  28 
  29 // Information about the protection of the page at address '0' on this os.
  30 static bool zero_page_read_protected() { return true; }
  31 
  32 // File conventions
  33 static const char* file_separator() { return "\\"; }
  34 static const char* line_separator() { return "\r\n"; }
  35 static const char* path_separator() { return ";"; }
  36 
  37 class win32 {
  38   friend class os;
  39   friend unsigned __stdcall java_start(class Thread*);
  40 
  41  protected:
  42   static int    _vm_page_size;
  43   static int    _vm_allocation_granularity;
  44   static int    _processor_type;
  45   static int    _processor_level;
  46   static julong _physical_memory;
  47   static size_t _default_stack_size;
  48   static bool   _is_nt;
  49   static bool   _is_windows_2003;
  50   static bool   _is_windows_server;
  51   static bool   _has_exit_bug;
  52   static bool   _has_performance_count;
  53 
  54   static void print_windows_version(outputStream* st);
  55 
  56  public:
  57   // Windows-specific interface:
  58   static void   initialize_system_info();
  59   static void   setmode_streams();
  60 
  61   // Processor info as provided by NT
  62   static int processor_type()  { return _processor_type;  }
  63   // Processor level may not be accurate on non-NT systems
  64   static int processor_level() {
  65     assert(is_nt(), "use vm_version instead");
  66     return _processor_level;
  67   }
  68   static julong available_memory();
  69   static julong physical_memory() { return _physical_memory; }
  70 
  71   // load dll from Windows system directory or Windows directory
  72   static HINSTANCE load_Windows_dll(const char* name, char *ebuf, int ebuflen);
  73 
  74  private:
  75   enum Ept { EPT_THREAD, EPT_PROCESS, EPT_PROCESS_DIE };
  76   // Wrapper around _endthreadex(), exit() and _exit()
  77   static int exit_process_or_thread(Ept what, int exit_code);
  78 
  79   static void initialize_performance_counter();
  80 
  81  public:
  82   // Generic interface:
  83 
  84   // Trace number of created threads
  85   static          intx  _os_thread_limit;
  86   static volatile intx  _os_thread_count;
  87 
  88   // Tells whether the platform is NT or Windown95
  89   static bool is_nt() { return _is_nt; }
  90 
  91   // Tells whether this is a server version of Windows
  92   static bool is_windows_server() { return _is_windows_server; }
  93 
  94   // Tells whether the platform is Windows 2003
  95   static bool is_windows_2003() { return _is_windows_2003; }
  96 
  97   // Tells whether there can be the race bug during process exit on this platform
  98   static bool has_exit_bug() { return _has_exit_bug; }
  99 
 100   // Returns the byte size of a virtual memory page
 101   static int vm_page_size() { return _vm_page_size; }
 102 
 103   // Returns the size in bytes of memory blocks which can be allocated.
 104   static int vm_allocation_granularity() { return _vm_allocation_granularity; }
 105 
 106   // Read the headers for the executable that started the current process into
 107   // the structure passed in (see winnt.h).
 108   static void read_executable_headers(PIMAGE_NT_HEADERS);
 109 
 110   // Default stack size for the current process.
 111   static size_t default_stack_size() { return _default_stack_size; }
 112 
 113 #ifndef _WIN64
 114   // A wrapper to install a structured exception handler for fast JNI accesors.
 115   static address fast_jni_accessor_wrapper(BasicType);
 116 #endif
 117 
 118   static void call_test_func_with_wrapper(void (*funcPtr)(void));
 119 
 120   // filter function to ignore faults on serializations page
 121   static LONG WINAPI serialize_fault_filter(struct _EXCEPTION_POINTERS* e);
 122 };
 123 
 124 /*
 125  * Crash protection for the watcher thread. Wrap the callback
 126  * with a __try { call() }
 127  * To be able to use this - don't take locks, don't rely on destructors,
 128  * don't make OS library calls, don't allocate memory, don't print,
 129  * don't call code that could leave the heap / memory in an inconsistent state,
 130  * or anything else where we are not in control if we suddenly jump out.
 131  */
 132 class WatcherThreadCrashProtection : public StackObj {
 133 public:
 134   WatcherThreadCrashProtection();
 135   bool call(os::CrashProtectionCallback& cb);
 136 };
 137 
 138 class PlatformEvent : public CHeapObj<mtInternal> {
 139   private:
 140     double CachePad [4] ;   // increase odds that _Event is sole occupant of cache line
 141     volatile int _Event ;
 142     HANDLE _ParkHandle ;
 143 
 144   public:       // TODO-FIXME: make dtor private
 145     ~PlatformEvent() { guarantee (0, "invariant") ; }
 146 
 147   public:
 148     PlatformEvent() {
 149       _Event   = 0 ;
 150       _ParkHandle = CreateEvent (NULL, false, false, NULL) ;
 151       guarantee (_ParkHandle != NULL, "invariant") ;
 152     }
 153 
 154     // Exercise caution using reset() and fired() - they may require MEMBARs
 155     void reset() { _Event = 0 ; }
 156     int  fired() { return _Event; }
 157     void park () ;
 158     void unpark () ;
 159     int  park (jlong millis) ;
 160 } ;
 161 
 162 
 163 
 164 class PlatformParker : public CHeapObj<mtInternal> {
 165   protected:
 166     HANDLE _ParkEvent ;
 167 
 168   public:
 169     ~PlatformParker () { guarantee (0, "invariant") ; }
 170     PlatformParker  () {
 171       _ParkEvent = CreateEvent (NULL, true, false, NULL) ;
 172       guarantee (_ParkEvent != NULL, "invariant") ;
 173     }
 174 
 175 } ;
 176 
 177 // JDK7 requires VS2010
 178 #if _MSC_VER < 1600
 179 #define JDK6_OR_EARLIER 1
 180 #endif
 181 
 182 
 183 
 184 class WinSock2Dll: AllStatic {
 185 public:
 186   static BOOL WSAStartup(WORD, LPWSADATA);
 187   static struct hostent* gethostbyname(const char *name);
 188   static BOOL WinSock2Available();
 189 #ifdef JDK6_OR_EARLIER
 190 private:
 191   static int (PASCAL FAR* _WSAStartup)(WORD, LPWSADATA);
 192   static struct hostent *(PASCAL FAR *_gethostbyname)(...);
 193   static BOOL initialized;
 194 
 195   static void initialize();
 196 #endif
 197 };
 198 
 199 class Kernel32Dll: AllStatic {
 200 public:
 201   static BOOL SwitchToThread();
 202   static SIZE_T GetLargePageMinimum();
 203 
 204   static BOOL SwitchToThreadAvailable();
 205   static BOOL GetLargePageMinimumAvailable();
 206 
 207   // Help tools
 208   static BOOL HelpToolsAvailable();
 209   static HANDLE CreateToolhelp32Snapshot(DWORD,DWORD);
 210   static BOOL Module32First(HANDLE,LPMODULEENTRY32);
 211   static BOOL Module32Next(HANDLE,LPMODULEENTRY32);
 212 
 213   static BOOL GetNativeSystemInfoAvailable();
 214   static void GetNativeSystemInfo(LPSYSTEM_INFO);
 215 
 216   // NUMA calls
 217   static BOOL NumaCallsAvailable();
 218   static LPVOID VirtualAllocExNuma(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
 219   static BOOL GetNumaHighestNodeNumber(PULONG);
 220   static BOOL GetNumaNodeProcessorMask(UCHAR, PULONGLONG);
 221 
 222   // Stack walking
 223   static USHORT RtlCaptureStackBackTrace(ULONG, ULONG, PVOID*, PULONG);
 224 
 225 private:
 226   // GetLargePageMinimum available on Windows Vista/Windows Server 2003
 227   // and later
 228   // NUMA calls available Windows Vista/WS2008 and later
 229 
 230   static SIZE_T (WINAPI *_GetLargePageMinimum)(void);
 231   static LPVOID (WINAPI *_VirtualAllocExNuma) (HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
 232   static BOOL (WINAPI *_GetNumaHighestNodeNumber) (PULONG);
 233   static BOOL (WINAPI *_GetNumaNodeProcessorMask) (UCHAR, PULONGLONG);
 234   static USHORT (WINAPI *_RtlCaptureStackBackTrace)(ULONG, ULONG, PVOID*, PULONG);
 235   static BOOL initialized;
 236 
 237   static void initialize();
 238   static void initializeCommon();
 239 
 240 #ifdef JDK6_OR_EARLIER
 241 private:
 242   static BOOL (WINAPI *_SwitchToThread)(void);
 243   static HANDLE (WINAPI* _CreateToolhelp32Snapshot)(DWORD,DWORD);
 244   static BOOL (WINAPI* _Module32First)(HANDLE,LPMODULEENTRY32);
 245   static BOOL (WINAPI* _Module32Next)(HANDLE,LPMODULEENTRY32);
 246   static void (WINAPI *_GetNativeSystemInfo)(LPSYSTEM_INFO);
 247 #endif
 248 
 249 };
 250 
 251 class Advapi32Dll: AllStatic {
 252 public:
 253   static BOOL AdjustTokenPrivileges(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
 254   static BOOL OpenProcessToken(HANDLE, DWORD, PHANDLE);
 255   static BOOL LookupPrivilegeValue(LPCTSTR, LPCTSTR, PLUID);
 256 
 257   static BOOL AdvapiAvailable();
 258 
 259 #ifdef JDK6_OR_EARLIER
 260 private:
 261   static BOOL (WINAPI *_AdjustTokenPrivileges)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
 262   static BOOL (WINAPI *_OpenProcessToken)(HANDLE, DWORD, PHANDLE);
 263   static BOOL (WINAPI *_LookupPrivilegeValue)(LPCTSTR, LPCTSTR, PLUID);
 264   static BOOL initialized;
 265 
 266   static void initialize();
 267 #endif
 268 };
 269 
 270 class PSApiDll: AllStatic {
 271 public:
 272   static BOOL EnumProcessModules(HANDLE, HMODULE *, DWORD, LPDWORD);
 273   static DWORD GetModuleFileNameEx(HANDLE, HMODULE, LPTSTR, DWORD);
 274   static BOOL GetModuleInformation(HANDLE, HMODULE, LPMODULEINFO, DWORD);
 275 
 276   static BOOL PSApiAvailable();
 277 
 278 #ifdef JDK6_OR_EARLIER
 279 private:
 280   static BOOL (WINAPI *_EnumProcessModules)(HANDLE, HMODULE *, DWORD, LPDWORD);
 281   static BOOL (WINAPI *_GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);;
 282   static BOOL (WINAPI *_GetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
 283   static BOOL initialized;
 284 
 285   static void initialize();
 286 #endif
 287 };
 288 
 289 #endif // OS_WINDOWS_VM_OS_WINDOWS_HPP