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