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