1 /*
   2  * Copyright (c) 1999, 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_BSD_VM_OS_BSD_HPP
  26 #define OS_BSD_VM_OS_BSD_HPP
  27 
  28 // Bsd_OS defines the interface to Bsd operating systems
  29 
  30 // Information about the protection of the page at address '0' on this os.
  31 static bool zero_page_read_protected() { return true; }
  32 
  33 /* pthread_getattr_np comes with BsdThreads-0.9-7 on RedHat 7.1 */
  34 typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
  35 
  36 #ifdef __APPLE__
  37 // Mac OS X doesn't support clock_gettime. Stub out the type, it is
  38 // unused
  39 typedef int clockid_t;
  40 #endif
  41 
  42 class Bsd {
  43   friend class os;
  44 
  45   // For signal-chaining
  46 #define MAXSIGNUM 32
  47   static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
  48   static unsigned int sigs;             // mask of signals that have
  49                                         // preinstalled signal handlers
  50   static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
  51                                         // __sigaction(), signal() is loaded
  52   static struct sigaction *(*get_signal_action)(int);
  53   static struct sigaction *get_preinstalled_handler(int);
  54   static void save_preinstalled_handler(int, struct sigaction&);
  55 
  56   static void check_signal_handler(int sig);
  57 
  58   // For signal flags diagnostics
  59   static int sigflags[MAXSIGNUM];
  60 
  61 #ifdef __APPLE__
  62   // mach_absolute_time
  63   static mach_timebase_info_data_t _timebase_info;
  64   static volatile uint64_t         _max_abstime;
  65 #else
  66   static int (*_clock_gettime)(clockid_t, struct timespec *);
  67 #endif
  68 
  69   static GrowableArray<int>* _cpu_to_node;
  70 
  71  protected:
  72 
  73   static julong _physical_memory;
  74   static pthread_t _main_thread;
  75   static int _page_size;
  76 
  77   static julong available_memory();
  78   static julong physical_memory() { return _physical_memory; }
  79   static void initialize_system_info();
  80 
  81   static bool supports_variable_stack_size();
  82 
  83   static void rebuild_cpu_to_node_map();
  84   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
  85 
  86   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
  87 
  88  public:
  89 
  90   static void init_thread_fpu_state();
  91   static pthread_t main_thread(void)                                { return _main_thread; }
  92 
  93   static void hotspot_sigmask(Thread* thread);
  94 
  95   static pid_t gettid();
  96 
  97   static int page_size(void)                                        { return _page_size; }
  98   static void set_page_size(int val)                                { _page_size = val; }
  99 
 100   static address   ucontext_get_pc(ucontext_t* uc);
 101   static intptr_t* ucontext_get_sp(ucontext_t* uc);
 102   static intptr_t* ucontext_get_fp(ucontext_t* uc);
 103 
 104   // For Analyzer Forte AsyncGetCallTrace profiling support:
 105   //
 106   // This interface should be declared in os_bsd_i486.hpp, but
 107   // that file provides extensions to the os class and not the
 108   // Bsd class.
 109   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
 110     intptr_t** ret_sp, intptr_t** ret_fp);
 111 
 112   // This boolean allows users to forward their own non-matching signals
 113   // to JVM_handle_bsd_signal, harmlessly.
 114   static bool signal_handlers_are_installed;
 115 
 116   static int get_our_sigflags(int);
 117   static void set_our_sigflags(int, int);
 118   static void signal_sets_init();
 119   static void install_signal_handlers();
 120   static void set_signal_handler(int, bool);
 121   static bool is_sig_ignored(int sig);
 122 
 123   static sigset_t* unblocked_signals();
 124   static sigset_t* vm_signals();
 125   static sigset_t* allowdebug_blocked_signals();
 126 
 127   // For signal-chaining
 128   static struct sigaction *get_chained_signal_action(int sig);
 129   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 130 
 131   // Minimum stack size a thread can be created with (allowing
 132   // the VM to completely create the thread and enter user code)
 133   static size_t min_stack_allowed;
 134 
 135   // Return default stack size or guard size for the specified thread type
 136   static size_t default_stack_size(os::ThreadType thr_type);
 137   static size_t default_guard_size(os::ThreadType thr_type);
 138 
 139   // Real-time clock functions
 140   static void clock_init(void);
 141 
 142   static inline bool supports_monotonic_clock() {
 143 #ifdef __APPLE__
 144     return true;
 145 #else
 146     return _clock_gettime != NULL;
 147 #endif
 148   }
 149 
 150   // Stack repair handling
 151 
 152   // none present
 153 
 154   // BsdThreads work-around for 6292965
 155   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
 156 
 157 private:
 158   typedef int (*sched_getcpu_func_t)(void);
 159   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 160   typedef int (*numa_max_node_func_t)(void);
 161   typedef int (*numa_available_func_t)(void);
 162   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 163   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 164 
 165   static sched_getcpu_func_t _sched_getcpu;
 166   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 167   static numa_max_node_func_t _numa_max_node;
 168   static numa_available_func_t _numa_available;
 169   static numa_tonode_memory_func_t _numa_tonode_memory;
 170   static numa_interleave_memory_func_t _numa_interleave_memory;
 171   static unsigned long* _numa_all_nodes;
 172 
 173   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 174   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 175   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 176   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 177   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 178   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 179   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 180 public:
 181   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 182   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 183     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 184   }
 185   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 186   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 187   static int numa_tonode_memory(void *start, size_t size, int node) {
 188     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 189   }
 190   static void numa_interleave_memory(void *start, size_t size) {
 191     if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 192       _numa_interleave_memory(start, size, _numa_all_nodes);
 193     }
 194   }
 195   static int get_node_by_cpu(int cpu_id);
 196 };
 197 
 198 
 199 class PlatformEvent : public CHeapObj<mtInternal> {
 200   private:
 201     double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
 202     volatile int _Event ;
 203     volatile int _nParked ;
 204     pthread_mutex_t _mutex  [1] ;
 205     pthread_cond_t  _cond   [1] ;
 206     double PostPad  [2] ;
 207     Thread * _Assoc ;
 208 
 209   public:       // TODO-FIXME: make dtor private
 210     ~PlatformEvent() { guarantee (0, "invariant") ; }
 211 
 212   public:
 213     PlatformEvent() {
 214       int status;
 215       status = pthread_cond_init (_cond, NULL);
 216       assert_status(status == 0, status, "cond_init");
 217       status = pthread_mutex_init (_mutex, NULL);
 218       assert_status(status == 0, status, "mutex_init");
 219       _Event   = 0 ;
 220       _nParked = 0 ;
 221       _Assoc   = NULL ;
 222     }
 223 
 224     // Use caution with reset() and fired() -- they may require MEMBARs
 225     void reset() { _Event = 0 ; }
 226     int  fired() { return _Event; }
 227     void park () ;
 228     void unpark () ;
 229     int  TryPark () ;
 230     int  park (jlong millis) ;
 231     void SetAssociation (Thread * a) { _Assoc = a ; }
 232 };
 233 
 234 class PlatformParker : public CHeapObj<mtInternal> {
 235   protected:
 236     pthread_mutex_t _mutex [1] ;
 237     pthread_cond_t  _cond  [1] ;
 238 
 239   public:       // TODO-FIXME: make dtor private
 240     ~PlatformParker() { guarantee (0, "invariant") ; }
 241 
 242   public:
 243     PlatformParker() {
 244       int status;
 245       status = pthread_cond_init (_cond, NULL);
 246       assert_status(status == 0, status, "cond_init");
 247       status = pthread_mutex_init (_mutex, NULL);
 248       assert_status(status == 0, status, "mutex_init");
 249     }
 250 };
 251 
 252 #endif // OS_BSD_VM_OS_BSD_HPP