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_LINUX_VM_OS_LINUX_HPP
  26 #define OS_LINUX_VM_OS_LINUX_HPP
  27 
  28 // Linux_OS defines the interface to Linux operating systems
  29 
  30 /* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
  31 typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
  32 
  33 // Information about the protection of the page at address '0' on this os.
  34 static bool zero_page_read_protected() { return true; }
  35 
  36 class Linux {
  37   friend class os;
  38   friend class OSContainer;
  39   friend class TestReserveMemorySpecial;
  40 
  41   // For signal-chaining
  42 #define MAXSIGNUM 32
  43   static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
  44   static unsigned int sigs;             // mask of signals that have
  45                                         // preinstalled signal handlers
  46   static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
  47                                         // __sigaction(), signal() is loaded
  48   static struct sigaction *(*get_signal_action)(int);
  49   static struct sigaction *get_preinstalled_handler(int);
  50   static void save_preinstalled_handler(int, struct sigaction&);
  51 
  52   static void check_signal_handler(int sig);
  53 
  54   // For signal flags diagnostics
  55   static int sigflags[MAXSIGNUM];
  56 
  57   static int (*_clock_gettime)(clockid_t, struct timespec *);
  58   static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
  59   static int (*_pthread_setname_np)(pthread_t, const char*);
  60 
  61   static address   _initial_thread_stack_bottom;
  62   static uintptr_t _initial_thread_stack_size;
  63 
  64   static const char *_glibc_version;
  65   static const char *_libpthread_version;
  66 
  67   static bool _is_floating_stack;
  68   static bool _is_NPTL;
  69   static bool _supports_fast_thread_cpu_time;
  70 
  71   static GrowableArray<int>* _cpu_to_node;
  72   static GrowableArray<int>* _nindex_to_node;
  73 
  74  protected:
  75 
  76   static julong _physical_memory;
  77   static pthread_t _main_thread;
  78   static Mutex* _createThread_lock;
  79   static int _page_size;
  80   static const int _vm_default_page_size;
  81 
  82   static julong available_memory();
  83   static julong physical_memory() { return _physical_memory; }
  84   static void set_physical_memory(julong phys_mem) { _physical_memory = phys_mem; }
  85   static int active_processor_count();
  86 
  87   static void initialize_system_info();
  88 
  89   static int commit_memory_impl(char* addr, size_t bytes, bool exec);
  90   static int commit_memory_impl(char* addr, size_t bytes,
  91                                 size_t alignment_hint, bool exec);
  92 
  93   static void set_glibc_version(const char *s)      { _glibc_version = s; }
  94   static void set_libpthread_version(const char *s) { _libpthread_version = s; }
  95 
  96   static bool supports_variable_stack_size();
  97 
  98   static void set_is_NPTL()                   { _is_NPTL = true;  }
  99   static void set_is_LinuxThreads()           { _is_NPTL = false; }
 100   static void set_is_floating_stack()         { _is_floating_stack = true; }
 101 
 102   static void rebuild_cpu_to_node_map();
 103   static void rebuild_nindex_to_node_map();
 104   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
 105   static GrowableArray<int>* nindex_to_node()  { return _nindex_to_node; }
 106 
 107   static size_t find_large_page_size();
 108   static size_t setup_large_page_size();
 109 
 110   static bool setup_large_page_type(size_t page_size);
 111   static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size);
 112   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
 113 
 114   static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec);
 115   static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec);
 116   static char* reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec);
 117   static char* reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec);
 118 
 119   static bool release_memory_special_impl(char* base, size_t bytes);
 120   static bool release_memory_special_shm(char* base, size_t bytes);
 121   static bool release_memory_special_huge_tlbfs(char* base, size_t bytes);
 122 
 123   static void print_full_memory_info(outputStream* st);
 124   static void print_container_info(outputStream* st);
 125   static void print_distro_info(outputStream* st);
 126   static void print_libversion_info(outputStream* st);
 127 
 128  public:
 129   static bool _stack_is_executable;
 130   static void *dlopen_helper(const char *name, char *ebuf, int ebuflen);
 131   static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen);
 132 
 133   static void init_thread_fpu_state();
 134   static int  get_fpu_control_word();
 135   static void set_fpu_control_word(int fpu_control);
 136   static pthread_t main_thread(void)                                { return _main_thread; }
 137   // returns kernel thread id (similar to LWP id on Solaris), which can be
 138   // used to access /proc
 139   static pid_t gettid();
 140   static void set_createThread_lock(Mutex* lk)                      { _createThread_lock = lk; }
 141   static Mutex* createThread_lock(void)                             { return _createThread_lock; }
 142   static void hotspot_sigmask(Thread* thread);
 143 
 144   static address   initial_thread_stack_bottom(void)                { return _initial_thread_stack_bottom; }
 145   static uintptr_t initial_thread_stack_size(void)                  { return _initial_thread_stack_size; }
 146 
 147   static int page_size(void)                                        { return _page_size; }
 148   static void set_page_size(int val)                                { _page_size = val; }
 149 
 150   static int vm_default_page_size(void)                             { return _vm_default_page_size; }
 151 
 152   static address   ucontext_get_pc(ucontext_t* uc);
 153   static intptr_t* ucontext_get_sp(ucontext_t* uc);
 154   static intptr_t* ucontext_get_fp(ucontext_t* uc);
 155 
 156   // For Analyzer Forte AsyncGetCallTrace profiling support:
 157   //
 158   // This interface should be declared in os_linux_i486.hpp, but
 159   // that file provides extensions to the os class and not the
 160   // Linux class.
 161   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
 162     intptr_t** ret_sp, intptr_t** ret_fp);
 163 
 164   // This boolean allows users to forward their own non-matching signals
 165   // to JVM_handle_linux_signal, harmlessly.
 166   static bool signal_handlers_are_installed;
 167 
 168   static int get_our_sigflags(int);
 169   static void set_our_sigflags(int, int);
 170   static void signal_sets_init();
 171   static void install_signal_handlers();
 172   static void set_signal_handler(int, bool);
 173   static bool is_sig_ignored(int sig);
 174 
 175   static sigset_t* unblocked_signals();
 176   static sigset_t* vm_signals();
 177   static sigset_t* allowdebug_blocked_signals();
 178 
 179   // For signal-chaining
 180   static struct sigaction *get_chained_signal_action(int sig);
 181   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 182 
 183   // GNU libc and libpthread version strings
 184   static const char *glibc_version()          { return _glibc_version; }
 185   static const char *libpthread_version()     { return _libpthread_version; }
 186 
 187   // NPTL or LinuxThreads?
 188   static bool is_LinuxThreads()               { return !_is_NPTL; }
 189   static bool is_NPTL()                       { return _is_NPTL;  }
 190 
 191   // NPTL is always floating stack. LinuxThreads could be using floating
 192   // stack or fixed stack.
 193   static bool is_floating_stack()             { return _is_floating_stack; }
 194 
 195   static void libpthread_init();
 196   static bool libnuma_init();
 197   static void* libnuma_dlsym(void* handle, const char* name);
 198   // libnuma v2 (libnuma_1.2) symbols
 199   static void* libnuma_v2_dlsym(void* handle, const char* name);
 200   // Minimum stack size a thread can be created with (allowing
 201   // the VM to completely create the thread and enter user code)
 202   static size_t min_stack_allowed;
 203 
 204   // Return default stack size or guard size for the specified thread type
 205   static size_t default_stack_size(os::ThreadType thr_type);
 206   static size_t default_guard_size(os::ThreadType thr_type);
 207 
 208   static void capture_initial_stack(size_t max_size);
 209 
 210   // Stack overflow handling
 211   static bool manually_expand_stack(JavaThread * t, address addr);
 212   static int max_register_window_saves_before_flushing();
 213 
 214   // Real-time clock functions
 215   static void clock_init(void);
 216 
 217   // fast POSIX clocks support
 218   static void fast_thread_clock_init(void);
 219 
 220   static inline bool supports_monotonic_clock() {
 221     return _clock_gettime != NULL;
 222   }
 223 
 224   static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
 225     return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
 226   }
 227 
 228   static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
 229     return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
 230   }
 231 
 232   static bool supports_fast_thread_cpu_time() {
 233     return _supports_fast_thread_cpu_time;
 234   }
 235 
 236   static jlong fast_thread_cpu_time(clockid_t clockid);
 237 
 238   // pthread_cond clock suppport
 239   private:
 240   static pthread_condattr_t _condattr[1];
 241 
 242   public:
 243   static pthread_condattr_t* condAttr() { return _condattr; }
 244 
 245   // Stack repair handling
 246 
 247   // none present
 248 
 249   // LinuxThreads work-around for 6292965
 250   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
 251 
 252 private:
 253   static void expand_stack_to(address bottom);
 254 
 255   typedef int (*sched_getcpu_func_t)(void);
 256   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 257   typedef int (*numa_max_node_func_t)(void);
 258   typedef int (*numa_num_configured_nodes_func_t)(void);
 259   typedef int (*numa_available_func_t)(void);
 260   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 261   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 262   typedef void (*numa_interleave_memory_v2_func_t)(void *start, size_t size, struct bitmask* mask);
 263 
 264   typedef void (*numa_set_bind_policy_func_t)(int policy);
 265   typedef int (*numa_bitmask_isbitset_func_t)(struct bitmask *bmp, unsigned int n);
 266   typedef int (*numa_distance_func_t)(int node1, int node2);
 267 
 268   static sched_getcpu_func_t _sched_getcpu;
 269   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 270   static numa_max_node_func_t _numa_max_node;
 271   static numa_num_configured_nodes_func_t _numa_num_configured_nodes;
 272   static numa_available_func_t _numa_available;
 273   static numa_tonode_memory_func_t _numa_tonode_memory;
 274   static numa_interleave_memory_func_t _numa_interleave_memory;
 275   static numa_interleave_memory_v2_func_t _numa_interleave_memory_v2;
 276   static numa_set_bind_policy_func_t _numa_set_bind_policy;
 277   static numa_bitmask_isbitset_func_t _numa_bitmask_isbitset;
 278   static numa_distance_func_t _numa_distance;
 279   static unsigned long* _numa_all_nodes;
 280   static struct bitmask* _numa_all_nodes_ptr;
 281   static struct bitmask* _numa_nodes_ptr;
 282 
 283   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 284   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 285   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 286   static void set_numa_num_configured_nodes(numa_num_configured_nodes_func_t func) { _numa_num_configured_nodes = func; }
 287   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 288   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 289   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 290   static void set_numa_interleave_memory_v2(numa_interleave_memory_v2_func_t func) { _numa_interleave_memory_v2 = func; }
 291   static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
 292   static void set_numa_bitmask_isbitset(numa_bitmask_isbitset_func_t func) { _numa_bitmask_isbitset = func; }
 293   static void set_numa_distance(numa_distance_func_t func) { _numa_distance = func; }
 294   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 295   static void set_numa_all_nodes_ptr(struct bitmask **ptr) { _numa_all_nodes_ptr = (ptr == NULL ? NULL : *ptr); }
 296   static void set_numa_nodes_ptr(struct bitmask **ptr) { _numa_nodes_ptr = (ptr == NULL ? NULL : *ptr); }
 297   static int sched_getcpu_syscall(void);
 298 public:
 299   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 300   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 301     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 302   }
 303   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 304   static int numa_num_configured_nodes() {
 305     return _numa_num_configured_nodes != NULL ? _numa_num_configured_nodes() : -1;
 306   }
 307   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 308   static int numa_tonode_memory(void *start, size_t size, int node) {
 309     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 310   }
 311   static void numa_interleave_memory(void *start, size_t size) {
 312     // Use v2 api if available
 313     if (_numa_interleave_memory_v2 != NULL && _numa_all_nodes_ptr != NULL) {
 314       _numa_interleave_memory_v2(start, size, _numa_all_nodes_ptr);
 315     } else if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 316       _numa_interleave_memory(start, size, _numa_all_nodes);
 317     }
 318   }
 319   static void numa_set_bind_policy(int policy) {
 320     if (_numa_set_bind_policy != NULL) {
 321       _numa_set_bind_policy(policy);
 322     }
 323   }
 324   static int numa_distance(int node1, int node2) {
 325     return _numa_distance != NULL ? _numa_distance(node1, node2) : -1;
 326   }
 327   static int get_node_by_cpu(int cpu_id);
 328   static int get_existing_num_nodes();
 329   // Check if numa node is configured (non-zero memory node).
 330   static bool isnode_in_configured_nodes(unsigned int n) {
 331     if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) {
 332       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
 333     } else
 334       return 0;
 335   }
 336   // Check if numa node exists in the system (including zero memory nodes).
 337   static bool isnode_in_existing_nodes(unsigned int n) {
 338     if (_numa_bitmask_isbitset != NULL && _numa_nodes_ptr != NULL) {
 339       return _numa_bitmask_isbitset(_numa_nodes_ptr, n);
 340     } else if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) {
 341       // Not all libnuma API v2 implement numa_nodes_ptr, so it's not possible
 342       // to trust the API version for checking its absence. On the other hand,
 343       // numa_nodes_ptr found in libnuma 2.0.9 and above is the only way to get
 344       // a complete view of all numa nodes in the system, hence numa_nodes_ptr
 345       // is used to handle CPU and nodes on architectures (like PowerPC) where
 346       // there can exist nodes with CPUs but no memory or vice-versa and the
 347       // nodes may be non-contiguous. For most of the architectures, like
 348       // x86_64, numa_node_ptr presents the same node set as found in
 349       // numa_all_nodes_ptr so it's possible to use numa_all_nodes_ptr as a
 350       // substitute.
 351       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
 352     } else
 353       return 0;
 354   }
 355 };
 356 
 357 
 358 class PlatformEvent : public CHeapObj<mtInternal> {
 359   private:
 360     double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
 361     volatile int _Event ;
 362     volatile int _nParked ;
 363     pthread_mutex_t _mutex  [1] ;
 364     pthread_cond_t  _cond   [1] ;
 365     double PostPad  [2] ;
 366     Thread * _Assoc ;
 367 
 368   public:       // TODO-FIXME: make dtor private
 369     ~PlatformEvent() { guarantee (0, "invariant") ; }
 370 
 371   public:
 372     PlatformEvent() {
 373       int status;
 374       status = pthread_cond_init (_cond, os::Linux::condAttr());
 375       assert_status(status == 0, status, "cond_init");
 376       status = pthread_mutex_init (_mutex, NULL);
 377       assert_status(status == 0, status, "mutex_init");
 378       _Event   = 0 ;
 379       _nParked = 0 ;
 380       _Assoc   = NULL ;
 381     }
 382 
 383     // Use caution with reset() and fired() -- they may require MEMBARs
 384     void reset() { _Event = 0 ; }
 385     int  fired() { return _Event; }
 386     void park () ;
 387     void unpark () ;
 388     int  TryPark () ;
 389     int  park (jlong millis) ; // relative timed-wait only
 390     void SetAssociation (Thread * a) { _Assoc = a ; }
 391 } ;
 392 
 393 class PlatformParker : public CHeapObj<mtInternal> {
 394   protected:
 395     enum {
 396         REL_INDEX = 0,
 397         ABS_INDEX = 1
 398     };
 399     int _cur_index;  // which cond is in use: -1, 0, 1
 400     pthread_mutex_t _mutex [1] ;
 401     pthread_cond_t  _cond  [2] ; // one for relative times and one for abs.
 402 
 403   public:       // TODO-FIXME: make dtor private
 404     ~PlatformParker() { guarantee (0, "invariant") ; }
 405 
 406   public:
 407     PlatformParker() {
 408       int status;
 409       status = pthread_cond_init (&_cond[REL_INDEX], os::Linux::condAttr());
 410       assert_status(status == 0, status, "cond_init rel");
 411       status = pthread_cond_init (&_cond[ABS_INDEX], NULL);
 412       assert_status(status == 0, status, "cond_init abs");
 413       status = pthread_mutex_init (_mutex, NULL);
 414       assert_status(status == 0, status, "mutex_init");
 415       _cur_index = -1; // mark as unused
 416     }
 417 };
 418 
 419 #endif // OS_LINUX_VM_OS_LINUX_HPP