1 /*
   2  * Copyright (c) 1997, 2018, 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 thread_native_entry(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_windows_server;
  49   static bool   _has_exit_bug;
  50 
  51   static void print_windows_version(outputStream* st);
  52 
  53  public:
  54   // Windows-specific interface:
  55   static void   initialize_system_info();
  56   static void   setmode_streams();
  57 
  58   // Processor info as provided by NT
  59   static int processor_type()  { return _processor_type;  }
  60   static int processor_level() {
  61     return _processor_level;
  62   }
  63   static julong available_memory();
  64   static julong physical_memory() { return _physical_memory; }
  65 
  66   // load dll from Windows system directory or Windows directory
  67   static HINSTANCE load_Windows_dll(const char* name, char *ebuf, int ebuflen);
  68 
  69  private:
  70   enum Ept { EPT_THREAD, EPT_PROCESS, EPT_PROCESS_DIE };
  71   // Wrapper around _endthreadex(), exit() and _exit()
  72   static int exit_process_or_thread(Ept what, int exit_code);
  73 
  74   static void initialize_performance_counter();
  75 
  76  public:
  77   // Generic interface:
  78 
  79   // Trace number of created threads
  80   static          intx  _os_thread_limit;
  81   static volatile intx  _os_thread_count;
  82 
  83   // Tells whether this is a server version of Windows
  84   static bool is_windows_server() { return _is_windows_server; }
  85 
  86   // Tells whether there can be the race bug during process exit on this platform
  87   static bool has_exit_bug() { return _has_exit_bug; }
  88 
  89   // Returns the byte size of a virtual memory page
  90   static int vm_page_size() { return _vm_page_size; }
  91 
  92   // Returns the size in bytes of memory blocks which can be allocated.
  93   static int vm_allocation_granularity() { return _vm_allocation_granularity; }
  94 
  95   // Read the headers for the executable that started the current process into
  96   // the structure passed in (see winnt.h).
  97   static void read_executable_headers(PIMAGE_NT_HEADERS);
  98 
  99   // Default stack size for the current process.
 100   static size_t default_stack_size() { return _default_stack_size; }
 101 
 102   static bool get_frame_at_stack_banging_point(JavaThread* thread,
 103                           struct _EXCEPTION_POINTERS* exceptionInfo,
 104                           address pc, frame* fr);
 105 
 106 #ifndef _WIN64
 107   // A wrapper to install a structured exception handler for fast JNI accesors.
 108   static address fast_jni_accessor_wrapper(BasicType);
 109 #endif
 110 
 111   // Fast access to current thread
 112 protected:
 113   static int _thread_ptr_offset;
 114 private:
 115   static void initialize_thread_ptr_offset();
 116 public:
 117   static inline void set_thread_ptr_offset(int offset) {
 118     _thread_ptr_offset = offset;
 119   }
 120   static inline int get_thread_ptr_offset() { return _thread_ptr_offset; }
 121 };
 122 
 123 /*
 124  * Crash protection for the watcher thread. Wrap the callback
 125  * with a __try { call() }
 126  * To be able to use this - don't take locks, don't rely on destructors,
 127  * don't make OS library calls, don't allocate memory, don't print,
 128  * don't call code that could leave the heap / memory in an inconsistent state,
 129  * or anything else where we are not in control if we suddenly jump out.
 130  */
 131 class ThreadCrashProtection : public StackObj {
 132 public:
 133   static bool is_crash_protected(Thread* thr) {
 134     return _crash_protection != NULL && _protected_thread == thr;
 135   }
 136 
 137   ThreadCrashProtection();
 138   bool call(os::CrashProtectionCallback& cb);
 139 private:
 140   static Thread* _protected_thread;
 141   static ThreadCrashProtection* _crash_protection;
 142   static volatile intptr_t _crash_mux;
 143 };
 144 
 145 class PlatformEvent : public CHeapObj<mtInternal> {
 146   private:
 147     double CachePad [4] ;   // increase odds that _Event is sole occupant of cache line
 148     volatile int _Event ;
 149     HANDLE _ParkHandle ;
 150 
 151   public:       // TODO-FIXME: make dtor private
 152     ~PlatformEvent() { guarantee (0, "invariant") ; }
 153 
 154   public:
 155     PlatformEvent() {
 156       _Event   = 0 ;
 157       _ParkHandle = CreateEvent (NULL, false, false, NULL) ;
 158       guarantee (_ParkHandle != NULL, "invariant") ;
 159     }
 160 
 161     // Exercise caution using reset() and fired() - they may require MEMBARs
 162     void reset() { _Event = 0 ; }
 163     int  fired() { return _Event; }
 164     void park () ;
 165     void unpark () ;
 166     int  park (jlong millis) ;
 167 } ;
 168 
 169 
 170 
 171 class PlatformParker : public CHeapObj<mtInternal> {
 172   protected:
 173     HANDLE _ParkEvent ;
 174 
 175   public:
 176     ~PlatformParker () { guarantee (0, "invariant") ; }
 177     PlatformParker  () {
 178       _ParkEvent = CreateEvent (NULL, true, false, NULL) ;
 179       guarantee (_ParkEvent != NULL, "invariant") ;
 180     }
 181 
 182 } ;
 183 
 184 #endif // OS_WINDOWS_VM_OS_WINDOWS_HPP