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   // Set PC into context. Needed for continuation after signal
 156   static void ucontext_set_pc(ucontext_t* uc, address pc);
 157 
 158   // For Analyzer Forte AsyncGetCallTrace profiling support:
 159   //
 160   // This interface should be declared in os_linux_i486.hpp, but
 161   // that file provides extensions to the os class and not the
 162   // Linux class.
 163   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
 164     intptr_t** ret_sp, intptr_t** ret_fp);
 165 
 166   // This boolean allows users to forward their own non-matching signals
 167   // to JVM_handle_linux_signal, harmlessly.
 168   static bool signal_handlers_are_installed;
 169 
 170   static int get_our_sigflags(int);
 171   static void set_our_sigflags(int, int);
 172   static void signal_sets_init();
 173   static void install_signal_handlers();
 174   static void set_signal_handler(int, bool);
 175   static bool is_sig_ignored(int sig);
 176 
 177   static sigset_t* unblocked_signals();
 178   static sigset_t* vm_signals();
 179   static sigset_t* allowdebug_blocked_signals();
 180 
 181   // For signal-chaining
 182   static struct sigaction *get_chained_signal_action(int sig);
 183   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 184 
 185   // GNU libc and libpthread version strings
 186   static const char *glibc_version()          { return _glibc_version; }
 187   static const char *libpthread_version()     { return _libpthread_version; }
 188 
 189   // NPTL or LinuxThreads?
 190   static bool is_LinuxThreads()               { return !_is_NPTL; }
 191   static bool is_NPTL()                       { return _is_NPTL;  }
 192 
 193   // NPTL is always floating stack. LinuxThreads could be using floating
 194   // stack or fixed stack.
 195   static bool is_floating_stack()             { return _is_floating_stack; }
 196 
 197   static void libpthread_init();
 198   static bool libnuma_init();
 199   static void* libnuma_dlsym(void* handle, const char* name);
 200   // libnuma v2 (libnuma_1.2) symbols
 201   static void* libnuma_v2_dlsym(void* handle, const char* name);
 202   // Minimum stack size a thread can be created with (allowing
 203   // the VM to completely create the thread and enter user code)
 204   static size_t min_stack_allowed;
 205 
 206   // Return default stack size or guard size for the specified thread type
 207   static size_t default_stack_size(os::ThreadType thr_type);
 208   static size_t default_guard_size(os::ThreadType thr_type);
 209 
 210   static void capture_initial_stack(size_t max_size);
 211 
 212   // Stack overflow handling
 213   static bool manually_expand_stack(JavaThread * t, address addr);
 214   static int max_register_window_saves_before_flushing();
 215 
 216   // Real-time clock functions
 217   static void clock_init(void);
 218 
 219   // fast POSIX clocks support
 220   static void fast_thread_clock_init(void);
 221 
 222   static inline bool supports_monotonic_clock() {
 223     return _clock_gettime != NULL;
 224   }
 225 
 226   static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
 227     return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
 228   }
 229 
 230   static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
 231     return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
 232   }
 233 
 234   static bool supports_fast_thread_cpu_time() {
 235     return _supports_fast_thread_cpu_time;
 236   }
 237 
 238   static jlong fast_thread_cpu_time(clockid_t clockid);
 239 
 240   // pthread_cond clock suppport
 241   private:
 242   static pthread_condattr_t _condattr[1];
 243 
 244   public:
 245   static pthread_condattr_t* condAttr() { return _condattr; }
 246 
 247   // Stack repair handling
 248 
 249   // none present
 250 
 251   // LinuxThreads work-around for 6292965
 252   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
 253 
 254 private:
 255   static void expand_stack_to(address bottom);
 256 
 257   typedef int (*sched_getcpu_func_t)(void);
 258   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 259   typedef int (*numa_max_node_func_t)(void);
 260   typedef int (*numa_num_configured_nodes_func_t)(void);
 261   typedef int (*numa_available_func_t)(void);
 262   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 263   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 264   typedef void (*numa_interleave_memory_v2_func_t)(void *start, size_t size, struct bitmask* mask);
 265 
 266   typedef void (*numa_set_bind_policy_func_t)(int policy);
 267   typedef int (*numa_bitmask_isbitset_func_t)(struct bitmask *bmp, unsigned int n);
 268   typedef int (*numa_distance_func_t)(int node1, int node2);
 269 
 270   static sched_getcpu_func_t _sched_getcpu;
 271   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 272   static numa_max_node_func_t _numa_max_node;
 273   static numa_num_configured_nodes_func_t _numa_num_configured_nodes;
 274   static numa_available_func_t _numa_available;
 275   static numa_tonode_memory_func_t _numa_tonode_memory;
 276   static numa_interleave_memory_func_t _numa_interleave_memory;
 277   static numa_interleave_memory_v2_func_t _numa_interleave_memory_v2;
 278   static numa_set_bind_policy_func_t _numa_set_bind_policy;
 279   static numa_bitmask_isbitset_func_t _numa_bitmask_isbitset;
 280   static numa_distance_func_t _numa_distance;
 281   static unsigned long* _numa_all_nodes;
 282   static struct bitmask* _numa_all_nodes_ptr;
 283   static struct bitmask* _numa_nodes_ptr;
 284 
 285   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 286   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 287   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 288   static void set_numa_num_configured_nodes(numa_num_configured_nodes_func_t func) { _numa_num_configured_nodes = func; }
 289   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 290   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 291   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 292   static void set_numa_interleave_memory_v2(numa_interleave_memory_v2_func_t func) { _numa_interleave_memory_v2 = func; }
 293   static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
 294   static void set_numa_bitmask_isbitset(numa_bitmask_isbitset_func_t func) { _numa_bitmask_isbitset = func; }
 295   static void set_numa_distance(numa_distance_func_t func) { _numa_distance = func; }
 296   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 297   static void set_numa_all_nodes_ptr(struct bitmask **ptr) { _numa_all_nodes_ptr = (ptr == NULL ? NULL : *ptr); }
 298   static void set_numa_nodes_ptr(struct bitmask **ptr) { _numa_nodes_ptr = (ptr == NULL ? NULL : *ptr); }
 299   static int sched_getcpu_syscall(void);
 300 public:
 301   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 302   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 303     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 304   }
 305   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 306   static int numa_num_configured_nodes() {
 307     return _numa_num_configured_nodes != NULL ? _numa_num_configured_nodes() : -1;
 308   }
 309   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 310   static int numa_tonode_memory(void *start, size_t size, int node) {
 311     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 312   }
 313   static void numa_interleave_memory(void *start, size_t size) {
 314     // Use v2 api if available
 315     if (_numa_interleave_memory_v2 != NULL && _numa_all_nodes_ptr != NULL) {
 316       _numa_interleave_memory_v2(start, size, _numa_all_nodes_ptr);
 317     } else if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 318       _numa_interleave_memory(start, size, _numa_all_nodes);
 319     }
 320   }
 321   static void numa_set_bind_policy(int policy) {
 322     if (_numa_set_bind_policy != NULL) {
 323       _numa_set_bind_policy(policy);
 324     }
 325   }
 326   static int numa_distance(int node1, int node2) {
 327     return _numa_distance != NULL ? _numa_distance(node1, node2) : -1;
 328   }
 329   static int get_node_by_cpu(int cpu_id);
 330   static int get_existing_num_nodes();
 331   // Check if numa node is configured (non-zero memory node).
 332   static bool isnode_in_configured_nodes(unsigned int n) {
 333     if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) {
 334       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
 335     } else
 336       return 0;
 337   }
 338   // Check if numa node exists in the system (including zero memory nodes).
 339   static bool isnode_in_existing_nodes(unsigned int n) {
 340     if (_numa_bitmask_isbitset != NULL && _numa_nodes_ptr != NULL) {
 341       return _numa_bitmask_isbitset(_numa_nodes_ptr, n);
 342     } else if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) {
 343       // Not all libnuma API v2 implement numa_nodes_ptr, so it's not possible
 344       // to trust the API version for checking its absence. On the other hand,
 345       // numa_nodes_ptr found in libnuma 2.0.9 and above is the only way to get
 346       // a complete view of all numa nodes in the system, hence numa_nodes_ptr
 347       // is used to handle CPU and nodes on architectures (like PowerPC) where
 348       // there can exist nodes with CPUs but no memory or vice-versa and the
 349       // nodes may be non-contiguous. For most of the architectures, like
 350       // x86_64, numa_node_ptr presents the same node set as found in
 351       // numa_all_nodes_ptr so it's possible to use numa_all_nodes_ptr as a
 352       // substitute.
 353       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
 354     } else
 355       return 0;
 356   }
 357 };
 358 
 359 
 360 class PlatformEvent : public CHeapObj<mtInternal> {
 361   private:
 362     double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
 363     volatile int _Event ;
 364     volatile int _nParked ;
 365     pthread_mutex_t _mutex  [1] ;
 366     pthread_cond_t  _cond   [1] ;
 367     double PostPad  [2] ;
 368     Thread * _Assoc ;
 369 
 370   public:       // TODO-FIXME: make dtor private
 371     ~PlatformEvent() { guarantee (0, "invariant") ; }
 372 
 373   public:
 374     PlatformEvent() {
 375       int status;
 376       status = pthread_cond_init (_cond, os::Linux::condAttr());
 377       assert_status(status == 0, status, "cond_init");
 378       status = pthread_mutex_init (_mutex, NULL);
 379       assert_status(status == 0, status, "mutex_init");
 380       _Event   = 0 ;
 381       _nParked = 0 ;
 382       _Assoc   = NULL ;
 383     }
 384 
 385     // Use caution with reset() and fired() -- they may require MEMBARs
 386     void reset() { _Event = 0 ; }
 387     int  fired() { return _Event; }
 388     void park () ;
 389     void unpark () ;
 390     int  TryPark () ;
 391     int  park (jlong millis) ; // relative timed-wait only
 392     void SetAssociation (Thread * a) { _Assoc = a ; }
 393 } ;
 394 
 395 class PlatformParker : public CHeapObj<mtInternal> {
 396   protected:
 397     enum {
 398         REL_INDEX = 0,
 399         ABS_INDEX = 1
 400     };
 401     int _cur_index;  // which cond is in use: -1, 0, 1
 402     pthread_mutex_t _mutex [1] ;
 403     pthread_cond_t  _cond  [2] ; // one for relative times and one for abs.
 404 
 405   public:       // TODO-FIXME: make dtor private
 406     ~PlatformParker() { guarantee (0, "invariant") ; }
 407 
 408   public:
 409     PlatformParker() {
 410       int status;
 411       status = pthread_cond_init (&_cond[REL_INDEX], os::Linux::condAttr());
 412       assert_status(status == 0, status, "cond_init rel");
 413       status = pthread_cond_init (&_cond[ABS_INDEX], NULL);
 414       assert_status(status == 0, status, "cond_init abs");
 415       status = pthread_mutex_init (_mutex, NULL);
 416       assert_status(status == 0, status, "mutex_init");
 417       _cur_index = -1; // mark as unused
 418     }
 419 };
 420 
 421 #endif // OS_LINUX_VM_OS_LINUX_HPP