1 /*
   2  * Copyright (c) 1997, 2017, 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 class win32 {
  33   friend class os;
  34 
  35  protected:
  36   static int    _vm_page_size;
  37   static int    _vm_allocation_granularity;
  38   static int    _processor_type;
  39   static int    _processor_level;
  40   static julong _physical_memory;
  41   static size_t _default_stack_size;
  42   static bool   _is_nt;
  43   static bool   _is_windows_2003;
  44   static bool   _is_windows_server;
  45 
  46   static void print_windows_version(outputStream* st);
  47 
  48  public:
  49   // Windows-specific interface:
  50   static void   initialize_system_info();
  51   static void   setmode_streams();
  52 
  53   // Processor info as provided by NT
  54   static int processor_type()  { return _processor_type;  }
  55   // Processor level may not be accurate on non-NT systems
  56   static int processor_level() {
  57     assert(is_nt(), "use vm_version instead");
  58     return _processor_level;
  59   }
  60   static julong available_memory();
  61   static julong physical_memory() { return _physical_memory; }
  62 
  63   // load dll from Windows system directory or Windows directory
  64   static HINSTANCE load_Windows_dll(const char* name, char *ebuf, int ebuflen);
  65 
  66  public:
  67   // Generic interface:
  68 
  69   // Trace number of created threads
  70   static          intx  _os_thread_limit;
  71   static volatile intx  _os_thread_count;
  72 
  73   // Tells whether the platform is NT or Windown95
  74   static bool is_nt() { return _is_nt; }
  75 
  76   // Tells whether this is a server version of Windows
  77   static bool is_windows_server() { return _is_windows_server; }
  78 
  79   // Tells whether the platform is Windows 2003
  80   static bool is_windows_2003() { return _is_windows_2003; }
  81 
  82   // Returns the byte size of a virtual memory page
  83   static int vm_page_size() { return _vm_page_size; }
  84 
  85   // Returns the size in bytes of memory blocks which can be allocated.
  86   static int vm_allocation_granularity() { return _vm_allocation_granularity; }
  87 
  88   // Read the headers for the executable that started the current process into
  89   // the structure passed in (see winnt.h).
  90   static void read_executable_headers(PIMAGE_NT_HEADERS);
  91 
  92   // Default stack size for the current process.
  93   static size_t default_stack_size() { return _default_stack_size; }
  94 
  95 #ifndef _WIN64
  96   // A wrapper to install a structured exception handler for fast JNI accesors.
  97   static address fast_jni_accessor_wrapper(BasicType);
  98 #endif
  99 
 100   static void call_test_func_with_wrapper(void (*funcPtr)(void));
 101 
 102   // filter function to ignore faults on serializations page
 103   static LONG WINAPI serialize_fault_filter(struct _EXCEPTION_POINTERS* e);
 104 };
 105 
 106 /*
 107  * Crash protection for the watcher thread. Wrap the callback
 108  * with a __try { call() }
 109  * To be able to use this - don't take locks, don't rely on destructors,
 110  * don't make OS library calls, don't allocate memory, don't print,
 111  * don't call code that could leave the heap / memory in an inconsistent state,
 112  * or anything else where we are not in control if we suddenly jump out.
 113  */
 114 class ThreadCrashProtection : public StackObj {
 115 public:
 116   static bool is_crash_protected(Thread* thr) {
 117     return _crash_protection != NULL && _protected_thread == thr;
 118   }
 119 
 120   ThreadCrashProtection();
 121   bool call(os::CrashProtectionCallback& cb);
 122 private:
 123   static Thread* _protected_thread;
 124   static ThreadCrashProtection* _crash_protection;
 125   static volatile intptr_t _crash_mux;
 126 };
 127 
 128 class PlatformEvent : public CHeapObj<mtInternal> {
 129   private:
 130     double CachePad [4] ;   // increase odds that _Event is sole occupant of cache line
 131     volatile int _Event ;
 132     HANDLE _ParkHandle ;
 133 
 134   public:       // TODO-FIXME: make dtor private
 135     ~PlatformEvent() { guarantee (0, "invariant") ; }
 136 
 137   public:
 138     PlatformEvent() {
 139       _Event   = 0 ;
 140       _ParkHandle = CreateEvent (NULL, false, false, NULL) ;
 141       guarantee (_ParkHandle != NULL, "invariant") ;
 142     }
 143 
 144     // Exercise caution using reset() and fired() - they may require MEMBARs
 145     void reset() { _Event = 0 ; }
 146     int  fired() { return _Event; }
 147     void park () ;
 148     void unpark () ;
 149     int  park (jlong millis) ;
 150 } ;
 151 
 152 
 153 
 154 class PlatformParker : public CHeapObj<mtInternal> {
 155   protected:
 156     HANDLE _ParkEvent ;
 157 
 158   public:
 159     ~PlatformParker () { guarantee (0, "invariant") ; }
 160     PlatformParker  () {
 161       _ParkEvent = CreateEvent (NULL, true, false, NULL) ;
 162       guarantee (_ParkEvent != NULL, "invariant") ;
 163     }
 164 
 165 } ;
 166 
 167 // JDK7 requires VS2010
 168 #if _MSC_VER < 1600
 169 #define JDK6_OR_EARLIER 1
 170 #endif
 171 
 172 
 173 
 174 class WinSock2Dll: AllStatic {
 175 public:
 176   static BOOL WSAStartup(WORD, LPWSADATA);
 177   static struct hostent* gethostbyname(const char *name);
 178   static BOOL WinSock2Available();
 179 #ifdef JDK6_OR_EARLIER
 180 private:
 181   static int (PASCAL FAR* _WSAStartup)(WORD, LPWSADATA);
 182   static struct hostent *(PASCAL FAR *_gethostbyname)(...);
 183   static BOOL initialized;
 184 
 185   static void initialize();
 186 #endif
 187 };
 188 
 189 class Kernel32Dll: AllStatic {
 190 public:
 191   static BOOL SwitchToThread();
 192   static SIZE_T GetLargePageMinimum();
 193 
 194   static BOOL SwitchToThreadAvailable();
 195   static BOOL GetLargePageMinimumAvailable();
 196 
 197   // Help tools
 198   static BOOL HelpToolsAvailable();
 199   static HANDLE CreateToolhelp32Snapshot(DWORD,DWORD);
 200   static BOOL Module32First(HANDLE,LPMODULEENTRY32);
 201   static BOOL Module32Next(HANDLE,LPMODULEENTRY32);
 202 
 203   static void GetNativeSystemInfo(LPSYSTEM_INFO);
 204 
 205   // NUMA calls
 206   static BOOL NumaCallsAvailable();
 207   static LPVOID VirtualAllocExNuma(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
 208   static BOOL GetNumaHighestNodeNumber(PULONG);
 209   static BOOL GetNumaNodeProcessorMask(UCHAR, PULONGLONG);
 210 
 211   // Stack walking
 212   static USHORT RtlCaptureStackBackTrace(ULONG, ULONG, PVOID*, PULONG);
 213 
 214 private:
 215   // GetLargePageMinimum available on Windows Vista/Windows Server 2003
 216   // and later
 217   // NUMA calls available Windows Vista/WS2008 and later
 218 
 219   static SIZE_T (WINAPI *_GetLargePageMinimum)(void);
 220   static LPVOID (WINAPI *_VirtualAllocExNuma) (HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
 221   static BOOL (WINAPI *_GetNumaHighestNodeNumber) (PULONG);
 222   static BOOL (WINAPI *_GetNumaNodeProcessorMask) (UCHAR, PULONGLONG);
 223   static USHORT (WINAPI *_RtlCaptureStackBackTrace)(ULONG, ULONG, PVOID*, PULONG);
 224   static BOOL initialized;
 225 
 226   static void initialize();
 227   static void initializeCommon();
 228 
 229 #ifdef JDK6_OR_EARLIER
 230 private:
 231   static BOOL (WINAPI *_SwitchToThread)(void);
 232   static HANDLE (WINAPI* _CreateToolhelp32Snapshot)(DWORD,DWORD);
 233   static BOOL (WINAPI* _Module32First)(HANDLE,LPMODULEENTRY32);
 234   static BOOL (WINAPI* _Module32Next)(HANDLE,LPMODULEENTRY32);
 235   static void (WINAPI *_GetNativeSystemInfo)(LPSYSTEM_INFO);
 236 #endif
 237 
 238 };
 239 
 240 class Advapi32Dll: AllStatic {
 241 public:
 242   static BOOL AdjustTokenPrivileges(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
 243   static BOOL OpenProcessToken(HANDLE, DWORD, PHANDLE);
 244   static BOOL LookupPrivilegeValue(LPCTSTR, LPCTSTR, PLUID);
 245 
 246   static BOOL AdvapiAvailable();
 247 
 248 #ifdef JDK6_OR_EARLIER
 249 private:
 250   static BOOL (WINAPI *_AdjustTokenPrivileges)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
 251   static BOOL (WINAPI *_OpenProcessToken)(HANDLE, DWORD, PHANDLE);
 252   static BOOL (WINAPI *_LookupPrivilegeValue)(LPCTSTR, LPCTSTR, PLUID);
 253   static BOOL initialized;
 254 
 255   static void initialize();
 256 #endif
 257 };
 258 
 259 class PSApiDll: AllStatic {
 260 public:
 261   static BOOL EnumProcessModules(HANDLE, HMODULE *, DWORD, LPDWORD);
 262   static DWORD GetModuleFileNameEx(HANDLE, HMODULE, LPTSTR, DWORD);
 263   static BOOL GetModuleInformation(HANDLE, HMODULE, LPMODULEINFO, DWORD);
 264 
 265   static BOOL PSApiAvailable();
 266 
 267 #ifdef JDK6_OR_EARLIER
 268 private:
 269   static BOOL (WINAPI *_EnumProcessModules)(HANDLE, HMODULE *, DWORD, LPDWORD);
 270   static BOOL (WINAPI *_GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);;
 271   static BOOL (WINAPI *_GetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
 272   static BOOL initialized;
 273 
 274   static void initialize();
 275 #endif
 276 };
 277 
 278 #endif // OS_WINDOWS_VM_OS_WINDOWS_HPP