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