1 /*
   2  * Copyright (c) 1999, 2017, 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 // Information about the protection of the page at address '0' on this os.
  31 static bool zero_page_read_protected() { return true; }
  32 
  33 class Linux {
  34   friend class os;
  35   friend class OSContainer;
  36   friend class TestReserveMemorySpecial;
  37 
  38   static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
  39                                         // __sigaction(), signal() is loaded
  40   static struct sigaction *(*get_signal_action)(int);
  41   static struct sigaction *get_preinstalled_handler(int);
  42   static void save_preinstalled_handler(int, struct sigaction&);
  43 
  44   static void check_signal_handler(int sig);
  45 
  46   static int (*_clock_gettime)(clockid_t, struct timespec *);
  47   static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
  48   static int (*_pthread_setname_np)(pthread_t, const char*);
  49 
  50   static address   _initial_thread_stack_bottom;
  51   static uintptr_t _initial_thread_stack_size;
  52 
  53   static const char *_glibc_version;
  54   static const char *_libpthread_version;
  55 
  56   static bool _supports_fast_thread_cpu_time;
  57 
  58   static GrowableArray<int>* _cpu_to_node;
  59   static GrowableArray<int>* _nindex_to_node;
  60 
  61   // 0x00000000 = uninitialized,
  62   // 0x01000000 = kernel version unknown,
  63   // otherwise a 32-bit number:
  64   // Ox00AABBCC
  65   // AA, Major Version
  66   // BB, Minor Version
  67   // CC, Fix   Version
  68   static uint32_t _os_version;
  69 
  70  protected:
  71 
  72   static julong _physical_memory;
  73   static pthread_t _main_thread;
  74   static Mutex* _createThread_lock;
  75   static int _page_size;
  76 
  77   static julong available_memory();
  78   static julong physical_memory() { return _physical_memory; }
  79   static void set_physical_memory(julong phys_mem) { _physical_memory = phys_mem; }
  80   static void initialize_system_info();
  81 
  82   static int commit_memory_impl(char* addr, size_t bytes, bool exec);
  83   static int commit_memory_impl(char* addr, size_t bytes,
  84                                 size_t alignment_hint, bool exec);
  85 
  86   static void set_glibc_version(const char *s)      { _glibc_version = s; }
  87   static void set_libpthread_version(const char *s) { _libpthread_version = s; }
  88 
  89   static void rebuild_cpu_to_node_map();
  90   static void rebuild_nindex_to_node_map();
  91   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
  92   static GrowableArray<int>* nindex_to_node()  { return _nindex_to_node; }
  93 
  94   static size_t find_large_page_size();
  95   static size_t setup_large_page_size();
  96 
  97   static bool setup_large_page_type(size_t page_size);
  98   static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size);
  99   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
 100 
 101   static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec);
 102   static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec);
 103   static char* reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec);
 104   static char* reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec);
 105 
 106   static bool release_memory_special_impl(char* base, size_t bytes);
 107   static bool release_memory_special_shm(char* base, size_t bytes);
 108   static bool release_memory_special_huge_tlbfs(char* base, size_t bytes);
 109 
 110   static void print_full_memory_info(outputStream* st);
 111   static void print_container_info(outputStream* st);
 112   static void print_distro_info(outputStream* st);
 113   static void print_libversion_info(outputStream* st);
 114 
 115  public:
 116   static bool _stack_is_executable;
 117   static void *dlopen_helper(const char *name, char *ebuf, int ebuflen);
 118   static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen);
 119 
 120   static void init_thread_fpu_state();
 121   static int  get_fpu_control_word();
 122   static void set_fpu_control_word(int fpu_control);
 123   static pthread_t main_thread(void)                                { return _main_thread; }
 124   // returns kernel thread id (similar to LWP id on Solaris), which can be
 125   // used to access /proc
 126   static pid_t gettid();
 127   static void set_createThread_lock(Mutex* lk)                      { _createThread_lock = lk; }
 128   static Mutex* createThread_lock(void)                             { return _createThread_lock; }
 129   static void hotspot_sigmask(Thread* thread);
 130 
 131   static address   initial_thread_stack_bottom(void)                { return _initial_thread_stack_bottom; }
 132   static uintptr_t initial_thread_stack_size(void)                  { return _initial_thread_stack_size; }
 133   static bool is_initial_thread(void);
 134 
 135   static int page_size(void)                                        { return _page_size; }
 136   static void set_page_size(int val)                                { _page_size = val; }
 137 
 138   static address   ucontext_get_pc(const ucontext_t* uc);
 139   static void ucontext_set_pc(ucontext_t* uc, address pc);
 140   static intptr_t* ucontext_get_sp(const ucontext_t* uc);
 141   static intptr_t* ucontext_get_fp(const ucontext_t* uc);
 142 
 143   // For Analyzer Forte AsyncGetCallTrace profiling support:
 144   //
 145   // This interface should be declared in os_linux_i486.hpp, but
 146   // that file provides extensions to the os class and not the
 147   // Linux class.
 148   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, const ucontext_t* uc,
 149                                               intptr_t** ret_sp, intptr_t** ret_fp);
 150 
 151   static bool get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr);
 152 
 153   // This boolean allows users to forward their own non-matching signals
 154   // to JVM_handle_linux_signal, harmlessly.
 155   static bool signal_handlers_are_installed;
 156 
 157   static int get_our_sigflags(int);
 158   static void set_our_sigflags(int, int);
 159   static void signal_sets_init();
 160   static void install_signal_handlers();
 161   static void set_signal_handler(int, bool);
 162   static bool is_sig_ignored(int sig);
 163 
 164   static sigset_t* unblocked_signals();
 165   static sigset_t* vm_signals();
 166 
 167   // For signal-chaining
 168   static struct sigaction *get_chained_signal_action(int sig);
 169   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 170 
 171   // GNU libc and libpthread version strings
 172   static const char *glibc_version()          { return _glibc_version; }
 173   static const char *libpthread_version()     { return _libpthread_version; }
 174 
 175   static void libpthread_init();
 176   static void sched_getcpu_init();
 177   static bool libnuma_init();
 178   static void* libnuma_dlsym(void* handle, const char* name);
 179   // libnuma v2 (libnuma_1.2) symbols
 180   static void* libnuma_v2_dlsym(void* handle, const char* name);
 181 
 182   // Return default guard size for the specified thread type
 183   static size_t default_guard_size(os::ThreadType thr_type);
 184 
 185   static void capture_initial_stack(size_t max_size);
 186 
 187   // Stack overflow handling
 188   static bool manually_expand_stack(JavaThread * t, address addr);
 189   static int max_register_window_saves_before_flushing();
 190 
 191   // Real-time clock functions
 192   static void clock_init(void);
 193 
 194   // fast POSIX clocks support
 195   static void fast_thread_clock_init(void);
 196 
 197   static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
 198     return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
 199   }
 200 
 201   static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
 202     return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
 203   }
 204 
 205   static bool supports_fast_thread_cpu_time() {
 206     return _supports_fast_thread_cpu_time;
 207   }
 208 
 209   static jlong fast_thread_cpu_time(clockid_t clockid);
 210 
 211   static void initialize_os_info();
 212   static bool os_version_is_known();
 213   static uint32_t os_version();
 214 
 215   // Stack repair handling
 216 
 217   // none present
 218 
 219  private:
 220   typedef int (*sched_getcpu_func_t)(void);
 221   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 222   typedef int (*numa_max_node_func_t)(void);
 223   typedef int (*numa_num_configured_nodes_func_t)(void);
 224   typedef int (*numa_available_func_t)(void);
 225   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 226   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 227   typedef void (*numa_interleave_memory_v2_func_t)(void *start, size_t size, struct bitmask* mask);
 228 
 229   typedef void (*numa_set_bind_policy_func_t)(int policy);
 230   typedef int (*numa_bitmask_isbitset_func_t)(struct bitmask *bmp, unsigned int n);
 231   typedef int (*numa_distance_func_t)(int node1, int node2);
 232 
 233   static sched_getcpu_func_t _sched_getcpu;
 234   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 235   static numa_max_node_func_t _numa_max_node;
 236   static numa_num_configured_nodes_func_t _numa_num_configured_nodes;
 237   static numa_available_func_t _numa_available;
 238   static numa_tonode_memory_func_t _numa_tonode_memory;
 239   static numa_interleave_memory_func_t _numa_interleave_memory;
 240   static numa_interleave_memory_v2_func_t _numa_interleave_memory_v2;
 241   static numa_set_bind_policy_func_t _numa_set_bind_policy;
 242   static numa_bitmask_isbitset_func_t _numa_bitmask_isbitset;
 243   static numa_distance_func_t _numa_distance;
 244   static unsigned long* _numa_all_nodes;
 245   static struct bitmask* _numa_all_nodes_ptr;
 246   static struct bitmask* _numa_nodes_ptr;
 247 
 248   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 249   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 250   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 251   static void set_numa_num_configured_nodes(numa_num_configured_nodes_func_t func) { _numa_num_configured_nodes = func; }
 252   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 253   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 254   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 255   static void set_numa_interleave_memory_v2(numa_interleave_memory_v2_func_t func) { _numa_interleave_memory_v2 = func; }
 256   static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
 257   static void set_numa_bitmask_isbitset(numa_bitmask_isbitset_func_t func) { _numa_bitmask_isbitset = func; }
 258   static void set_numa_distance(numa_distance_func_t func) { _numa_distance = func; }
 259   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 260   static void set_numa_all_nodes_ptr(struct bitmask **ptr) { _numa_all_nodes_ptr = *ptr; }
 261   static void set_numa_nodes_ptr(struct bitmask **ptr) { _numa_nodes_ptr = *ptr; }
 262   static int sched_getcpu_syscall(void);
 263  public:
 264   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 265   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 266     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 267   }
 268   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 269   static int numa_num_configured_nodes() {
 270     return _numa_num_configured_nodes != NULL ? _numa_num_configured_nodes() : -1;
 271   }
 272   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 273   static int numa_tonode_memory(void *start, size_t size, int node) {
 274     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 275   }
 276   static void numa_interleave_memory(void *start, size_t size) {
 277     // Use v2 api if available
 278     if (_numa_interleave_memory_v2 != NULL && _numa_all_nodes_ptr != NULL) {
 279       _numa_interleave_memory_v2(start, size, _numa_all_nodes_ptr);
 280     } else if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 281       _numa_interleave_memory(start, size, _numa_all_nodes);
 282     }
 283   }
 284   static void numa_set_bind_policy(int policy) {
 285     if (_numa_set_bind_policy != NULL) {
 286       _numa_set_bind_policy(policy);
 287     }
 288   }
 289   static int numa_distance(int node1, int node2) {
 290     return _numa_distance != NULL ? _numa_distance(node1, node2) : -1;
 291   }
 292   static int get_node_by_cpu(int cpu_id);
 293   static int get_existing_num_nodes();
 294   // Check if numa node is configured (non-zero memory node).
 295   static bool isnode_in_configured_nodes(unsigned int n) {
 296     if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) {
 297       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
 298     } else
 299       return 0;
 300   }
 301   // Check if numa node exists in the system (including zero memory nodes).
 302   static bool isnode_in_existing_nodes(unsigned int n) {
 303     if (_numa_bitmask_isbitset != NULL && _numa_nodes_ptr != NULL) {
 304       return _numa_bitmask_isbitset(_numa_nodes_ptr, n);
 305     } else
 306       return 0;
 307   }
 308 };
 309 
 310 #endif // OS_LINUX_VM_OS_LINUX_HPP