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