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