1 /*
   2  * Copyright (c) 1997, 2015, 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   // filter function to ignore faults on serializations page
 119   static LONG WINAPI serialize_fault_filter(struct _EXCEPTION_POINTERS* e);
 120 };
 121 
 122 /*
 123  * Crash protection for the watcher thread. Wrap the callback
 124  * with a __try { call() }
 125  * To be able to use this - don't take locks, don't rely on destructors,
 126  * don't make OS library calls, don't allocate memory, don't print,
 127  * don't call code that could leave the heap / memory in an inconsistent state,
 128  * or anything else where we are not in control if we suddenly jump out.
 129  */
 130 class WatcherThreadCrashProtection : public StackObj {
 131 public:
 132   WatcherThreadCrashProtection();
 133   bool call(os::CrashProtectionCallback& cb);
 134 };
 135 
 136 class PlatformEvent : public CHeapObj<mtInternal> {
 137   private:
 138     double CachePad [4] ;   // increase odds that _Event is sole occupant of cache line
 139     volatile int _Event ;
 140     HANDLE _ParkHandle ;
 141 
 142   public:       // TODO-FIXME: make dtor private
 143     ~PlatformEvent() { guarantee (0, "invariant") ; }
 144 
 145   public:
 146     PlatformEvent() {
 147       _Event   = 0 ;
 148       _ParkHandle = CreateEvent (NULL, false, false, NULL) ;
 149       guarantee (_ParkHandle != NULL, "invariant") ;
 150     }
 151 
 152     // Exercise caution using reset() and fired() - they may require MEMBARs
 153     void reset() { _Event = 0 ; }
 154     int  fired() { return _Event; }
 155     void park () ;
 156     void unpark () ;
 157     int  park (jlong millis) ;
 158 } ;
 159 
 160 
 161 
 162 class PlatformParker : public CHeapObj<mtInternal> {
 163   protected:
 164     HANDLE _ParkEvent ;
 165 
 166   public:
 167     ~PlatformParker () { guarantee (0, "invariant") ; }
 168     PlatformParker  () {
 169       _ParkEvent = CreateEvent (NULL, true, false, NULL) ;
 170       guarantee (_ParkEvent != NULL, "invariant") ;
 171     }
 172 
 173 } ;
 174 
 175 // JDK7 requires VS2010
 176 #if _MSC_VER < 1600
 177 #define JDK6_OR_EARLIER 1
 178 #endif
 179 
 180 
 181 
 182 class WinSock2Dll: AllStatic {
 183 public:
 184   static BOOL WSAStartup(WORD, LPWSADATA);
 185   static struct hostent* gethostbyname(const char *name);
 186   static BOOL WinSock2Available();
 187 #ifdef JDK6_OR_EARLIER
 188 private:
 189   static int (PASCAL FAR* _WSAStartup)(WORD, LPWSADATA);
 190   static struct hostent *(PASCAL FAR *_gethostbyname)(...);
 191   static BOOL initialized;
 192 
 193   static void initialize();
 194 #endif
 195 };
 196 
 197 class Kernel32Dll: AllStatic {
 198 public:
 199   static BOOL SwitchToThread();
 200   static SIZE_T GetLargePageMinimum();
 201 
 202   static BOOL SwitchToThreadAvailable();
 203   static BOOL GetLargePageMinimumAvailable();
 204 
 205   // Help tools
 206   static BOOL HelpToolsAvailable();
 207   static HANDLE CreateToolhelp32Snapshot(DWORD,DWORD);
 208   static BOOL Module32First(HANDLE,LPMODULEENTRY32);
 209   static BOOL Module32Next(HANDLE,LPMODULEENTRY32);
 210 
 211   static void GetNativeSystemInfo(LPSYSTEM_INFO);
 212 
 213   // NUMA calls
 214   static BOOL NumaCallsAvailable();
 215   static LPVOID VirtualAllocExNuma(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
 216   static BOOL GetNumaHighestNodeNumber(PULONG);
 217   static BOOL GetNumaNodeProcessorMask(UCHAR, PULONGLONG);
 218 
 219   // Stack walking
 220   static USHORT RtlCaptureStackBackTrace(ULONG, ULONG, PVOID*, PULONG);
 221 
 222 private:
 223   // GetLargePageMinimum available on Windows Vista/Windows Server 2003
 224   // and later
 225   // NUMA calls available Windows Vista/WS2008 and later
 226 
 227   static SIZE_T (WINAPI *_GetLargePageMinimum)(void);
 228   static LPVOID (WINAPI *_VirtualAllocExNuma) (HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
 229   static BOOL (WINAPI *_GetNumaHighestNodeNumber) (PULONG);
 230   static BOOL (WINAPI *_GetNumaNodeProcessorMask) (UCHAR, PULONGLONG);
 231   static USHORT (WINAPI *_RtlCaptureStackBackTrace)(ULONG, ULONG, PVOID*, PULONG);
 232   static BOOL initialized;
 233 
 234   static void initialize();
 235   static void initializeCommon();
 236 
 237 #ifdef JDK6_OR_EARLIER
 238 private:
 239   static BOOL (WINAPI *_SwitchToThread)(void);
 240   static HANDLE (WINAPI* _CreateToolhelp32Snapshot)(DWORD,DWORD);
 241   static BOOL (WINAPI* _Module32First)(HANDLE,LPMODULEENTRY32);
 242   static BOOL (WINAPI* _Module32Next)(HANDLE,LPMODULEENTRY32);
 243   static void (WINAPI *_GetNativeSystemInfo)(LPSYSTEM_INFO);
 244 #endif
 245 
 246 };
 247 
 248 class Advapi32Dll: AllStatic {
 249 public:
 250   static BOOL AdjustTokenPrivileges(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
 251   static BOOL OpenProcessToken(HANDLE, DWORD, PHANDLE);
 252   static BOOL LookupPrivilegeValue(LPCTSTR, LPCTSTR, PLUID);
 253 
 254   static BOOL AdvapiAvailable();
 255 
 256 #ifdef JDK6_OR_EARLIER
 257 private:
 258   static BOOL (WINAPI *_AdjustTokenPrivileges)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
 259   static BOOL (WINAPI *_OpenProcessToken)(HANDLE, DWORD, PHANDLE);
 260   static BOOL (WINAPI *_LookupPrivilegeValue)(LPCTSTR, LPCTSTR, PLUID);
 261   static BOOL initialized;
 262 
 263   static void initialize();
 264 #endif
 265 };
 266 
 267 class PSApiDll: AllStatic {
 268 public:
 269   static BOOL EnumProcessModules(HANDLE, HMODULE *, DWORD, LPDWORD);
 270   static DWORD GetModuleFileNameEx(HANDLE, HMODULE, LPTSTR, DWORD);
 271   static BOOL GetModuleInformation(HANDLE, HMODULE, LPMODULEINFO, DWORD);
 272 
 273   static BOOL PSApiAvailable();
 274 
 275 #ifdef JDK6_OR_EARLIER
 276 private:
 277   static BOOL (WINAPI *_EnumProcessModules)(HANDLE, HMODULE *, DWORD, LPDWORD);
 278   static BOOL (WINAPI *_GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);;
 279   static BOOL (WINAPI *_GetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
 280   static BOOL initialized;
 281 
 282   static void initialize();
 283 #endif
 284 };
 285 
 286 #endif // OS_WINDOWS_VM_OS_WINDOWS_HPP