1 /*
   2  * Copyright (c) 1999, 2010, 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 /* pthread_getattr_np comes with BsdThreads-0.9-7 on RedHat 7.1 */
  31 typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
  32 
  33 #ifdef __APPLE__
  34 // Mac OS X doesn't support clock_gettime. Stub out the type, it is
  35 // unused
  36 typedef int clockid_t;
  37 #endif
  38 
  39 class Bsd {
  40   friend class os;
  41 
  42   // For signal-chaining
  43 #define MAXSIGNUM 32
  44   static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
  45   static unsigned int sigs;             // mask of signals that have
  46                                         // preinstalled signal handlers
  47   static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
  48                                         // __sigaction(), signal() is loaded
  49   static struct sigaction *(*get_signal_action)(int);
  50   static struct sigaction *get_preinstalled_handler(int);
  51   static void save_preinstalled_handler(int, struct sigaction&);
  52 
  53   static void check_signal_handler(int sig);
  54 
  55   // For signal flags diagnostics
  56   static int sigflags[MAXSIGNUM];
  57 
  58   static int (*_clock_gettime)(clockid_t, struct timespec *);
  59 #ifndef _ALLBSD_SOURCE
  60   static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
  61 
  62   static address   _initial_thread_stack_bottom;
  63   static uintptr_t _initial_thread_stack_size;
  64 
  65   static const char *_glibc_version;
  66   static const char *_libpthread_version;
  67 
  68   static bool _is_floating_stack;
  69   static bool _is_NPTL;
  70   static bool _supports_fast_thread_cpu_time;
  71 #endif
  72 
  73   static GrowableArray<int>* _cpu_to_node;
  74 
  75  protected:
  76 
  77   static julong _physical_memory;
  78   static pthread_t _main_thread;
  79 #ifndef _ALLBSD_SOURCE
  80   static Mutex* _createThread_lock;
  81 #endif
  82   static int _page_size;
  83 
  84   static julong available_memory();
  85   static julong physical_memory() { return _physical_memory; }
  86   static void initialize_system_info();
  87 
  88 #ifndef _ALLBSD_SOURCE
  89   static void set_glibc_version(const char *s)      { _glibc_version = s; }
  90   static void set_libpthread_version(const char *s) { _libpthread_version = s; }
  91 #endif
  92 
  93   static bool supports_variable_stack_size();
  94 
  95 #ifndef _ALLBSD_SOURCE
  96   static void set_is_NPTL()                   { _is_NPTL = true;  }
  97   static void set_is_BsdThreads()           { _is_NPTL = false; }
  98   static void set_is_floating_stack()         { _is_floating_stack = true; }
  99 #endif
 100 
 101   static void rebuild_cpu_to_node_map();
 102   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
 103 
 104   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
 105 
 106  public:
 107 
 108   static void init_thread_fpu_state();
 109 #ifndef _ALLBSD_SOURCE
 110   static int  get_fpu_control_word();
 111   static void set_fpu_control_word(int fpu_control);
 112 #endif
 113   static pthread_t main_thread(void)                                { return _main_thread; }
 114 
 115 #ifndef _ALLBSD_SOURCE
 116   // returns kernel thread id (similar to LWP id on Solaris), which can be
 117   // used to access /proc
 118   static pid_t gettid();
 119   static void set_createThread_lock(Mutex* lk)                      { _createThread_lock = lk; }
 120   static Mutex* createThread_lock(void)                             { return _createThread_lock; }
 121 #endif
 122   static void hotspot_sigmask(Thread* thread);
 123 
 124 #ifndef _ALLBSD_SOURCE
 125   static address   initial_thread_stack_bottom(void)                { return _initial_thread_stack_bottom; }
 126   static uintptr_t initial_thread_stack_size(void)                  { return _initial_thread_stack_size; }
 127 #endif
 128   static bool is_initial_thread(void);
 129 
 130   static int page_size(void)                                        { return _page_size; }
 131   static void set_page_size(int val)                                { _page_size = val; }
 132 
 133   static address   ucontext_get_pc(ucontext_t* uc);
 134   static intptr_t* ucontext_get_sp(ucontext_t* uc);
 135   static intptr_t* ucontext_get_fp(ucontext_t* uc);
 136 
 137   // For Analyzer Forte AsyncGetCallTrace profiling support:
 138   //
 139   // This interface should be declared in os_bsd_i486.hpp, but
 140   // that file provides extensions to the os class and not the
 141   // Bsd class.
 142   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
 143     intptr_t** ret_sp, intptr_t** ret_fp);
 144 
 145   // This boolean allows users to forward their own non-matching signals
 146   // to JVM_handle_bsd_signal, harmlessly.
 147   static bool signal_handlers_are_installed;
 148 
 149   static int get_our_sigflags(int);
 150   static void set_our_sigflags(int, int);
 151   static void signal_sets_init();
 152   static void install_signal_handlers();
 153   static void set_signal_handler(int, bool);
 154   static bool is_sig_ignored(int sig);
 155 
 156   static sigset_t* unblocked_signals();
 157   static sigset_t* vm_signals();
 158   static sigset_t* allowdebug_blocked_signals();
 159 
 160   // For signal-chaining
 161   static struct sigaction *get_chained_signal_action(int sig);
 162   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 163 
 164 #ifndef _ALLBSD_SOURCE
 165   // GNU libc and libpthread version strings
 166   static const char *glibc_version()          { return _glibc_version; }
 167   static const char *libpthread_version()     { return _libpthread_version; }
 168 
 169   // NPTL or BsdThreads?
 170   static bool is_BsdThreads()               { return !_is_NPTL; }
 171   static bool is_NPTL()                       { return _is_NPTL;  }
 172 
 173   // NPTL is always floating stack. BsdThreads could be using floating
 174   // stack or fixed stack.
 175   static bool is_floating_stack()             { return _is_floating_stack; }
 176 
 177   static void libpthread_init();
 178   static bool libnuma_init();
 179   static void* libnuma_dlsym(void* handle, const char* name);
 180 #endif
 181   // Minimum stack size a thread can be created with (allowing
 182   // the VM to completely create the thread and enter user code)
 183   static size_t min_stack_allowed;
 184 
 185   // Return default stack size or guard size for the specified thread type
 186   static size_t default_stack_size(os::ThreadType thr_type);
 187   static size_t default_guard_size(os::ThreadType thr_type);
 188 
 189 #ifndef _ALLBSD_SOURCE
 190   static void capture_initial_stack(size_t max_size);
 191 
 192   // Stack overflow handling
 193   static bool manually_expand_stack(JavaThread * t, address addr);
 194   static int max_register_window_saves_before_flushing();
 195 #endif
 196 
 197   // Real-time clock functions
 198   static void clock_init(void);
 199 
 200 #ifndef _ALLBSD_SOURCE
 201   // fast POSIX clocks support
 202   static void fast_thread_clock_init(void);
 203 #endif
 204 
 205   static bool supports_monotonic_clock() {
 206     return _clock_gettime != NULL;
 207   }
 208 
 209   static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
 210     return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
 211   }
 212 
 213 #ifndef _ALLBSD_SOURCE
 214   static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
 215     return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
 216   }
 217 
 218   static bool supports_fast_thread_cpu_time() {
 219     return _supports_fast_thread_cpu_time;
 220   }
 221 
 222   static jlong fast_thread_cpu_time(clockid_t clockid);
 223 #endif
 224 
 225   // Stack repair handling
 226 
 227   // none present
 228 
 229   // BsdThreads work-around for 6292965
 230   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
 231 
 232 
 233   // Bsd suspend/resume support - this helper is a shadow of its former
 234   // self now that low-level suspension is barely used, and old workarounds
 235   // for BsdThreads are no longer needed.
 236   class SuspendResume {
 237   private:
 238     volatile int _suspend_action;
 239     // values for suspend_action:
 240     #define SR_NONE               (0x00)
 241     #define SR_SUSPEND            (0x01)  // suspend request
 242     #define SR_CONTINUE           (0x02)  // resume request
 243 
 244     volatile jint _state;
 245     // values for _state: + SR_NONE
 246     #define SR_SUSPENDED          (0x20)
 247   public:
 248     SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
 249 
 250     int suspend_action() const     { return _suspend_action; }
 251     void set_suspend_action(int x) { _suspend_action = x;    }
 252 
 253     // atomic updates for _state
 254     void set_suspended()           {
 255       jint temp, temp2;
 256       do {
 257         temp = _state;
 258         temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp);
 259       } while (temp2 != temp);
 260     }
 261     void clear_suspended()        {
 262       jint temp, temp2;
 263       do {
 264         temp = _state;
 265         temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp);
 266       } while (temp2 != temp);
 267     }
 268     bool is_suspended()            { return _state & SR_SUSPENDED;       }
 269 
 270     #undef SR_SUSPENDED
 271   };
 272 
 273 private:
 274   typedef int (*sched_getcpu_func_t)(void);
 275   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 276   typedef int (*numa_max_node_func_t)(void);
 277   typedef int (*numa_available_func_t)(void);
 278   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 279   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 280 
 281   static sched_getcpu_func_t _sched_getcpu;
 282   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 283   static numa_max_node_func_t _numa_max_node;
 284   static numa_available_func_t _numa_available;
 285   static numa_tonode_memory_func_t _numa_tonode_memory;
 286   static numa_interleave_memory_func_t _numa_interleave_memory;
 287   static unsigned long* _numa_all_nodes;
 288 
 289   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 290   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 291   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 292   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 293   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 294   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 295   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 296 public:
 297   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 298   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 299     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 300   }
 301   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 302   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 303   static int numa_tonode_memory(void *start, size_t size, int node) {
 304     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 305   }
 306   static void numa_interleave_memory(void *start, size_t size) {
 307     if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 308       _numa_interleave_memory(start, size, _numa_all_nodes);
 309     }
 310   }
 311   static int get_node_by_cpu(int cpu_id);
 312 };
 313 
 314 
 315 class PlatformEvent : public CHeapObj {
 316   private:
 317     double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
 318     volatile int _Event ;
 319     volatile int _nParked ;
 320     pthread_mutex_t _mutex  [1] ;
 321     pthread_cond_t  _cond   [1] ;
 322     double PostPad  [2] ;
 323     Thread * _Assoc ;
 324 
 325   public:       // TODO-FIXME: make dtor private
 326     ~PlatformEvent() { guarantee (0, "invariant") ; }
 327 
 328   public:
 329     PlatformEvent() {
 330       int status;
 331       status = pthread_cond_init (_cond, NULL);
 332       assert_status(status == 0, status, "cond_init");
 333       status = pthread_mutex_init (_mutex, NULL);
 334       assert_status(status == 0, status, "mutex_init");
 335       _Event   = 0 ;
 336       _nParked = 0 ;
 337       _Assoc   = NULL ;
 338     }
 339 
 340     // Use caution with reset() and fired() -- they may require MEMBARs
 341     void reset() { _Event = 0 ; }
 342     int  fired() { return _Event; }
 343     void park () ;
 344     void unpark () ;
 345     int  TryPark () ;
 346     int  park (jlong millis) ;
 347     void SetAssociation (Thread * a) { _Assoc = a ; }
 348 } ;
 349 
 350 class PlatformParker : public CHeapObj {
 351   protected:
 352     pthread_mutex_t _mutex [1] ;
 353     pthread_cond_t  _cond  [1] ;
 354 
 355   public:       // TODO-FIXME: make dtor private
 356     ~PlatformParker() { guarantee (0, "invariant") ; }
 357 
 358   public:
 359     PlatformParker() {
 360       int status;
 361       status = pthread_cond_init (_cond, NULL);
 362       assert_status(status == 0, status, "cond_init");
 363       status = pthread_mutex_init (_mutex, NULL);
 364       assert_status(status == 0, status, "mutex_init");
 365     }
 366 } ;
 367 
 368 #endif // OS_BSD_VM_OS_BSD_HPP