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_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 class Bsd {
  34   friend class os;
  35 
  36   // For signal-chaining
  37   static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
  38                                         // __sigaction(), signal() is loaded
  39   static struct sigaction *(*get_signal_action)(int);
  40   static struct sigaction *get_preinstalled_handler(int);
  41   static void save_preinstalled_handler(int, struct sigaction&);
  42 
  43   static void check_signal_handler(int sig);
  44 
  45 #ifdef __APPLE__
  46   // mach_absolute_time
  47   static mach_timebase_info_data_t _timebase_info;
  48   static volatile uint64_t         _max_abstime;
  49 #else
  50   static int (*_clock_gettime)(clockid_t, struct timespec *);
  51 #endif
  52 
  53   static GrowableArray<int>* _cpu_to_node;
  54 
  55  protected:
  56 
  57   static julong _physical_memory;
  58   static pthread_t _main_thread;
  59   static int _page_size;
  60 
  61   static julong available_memory();
  62   static julong physical_memory() { return _physical_memory; }
  63   static void initialize_system_info();
  64 
  65   static void rebuild_cpu_to_node_map();
  66   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
  67 
  68   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
  69 
  70  public:
  71 
  72   static void init_thread_fpu_state();
  73   static pthread_t main_thread(void)                                { return _main_thread; }
  74 
  75   static void hotspot_sigmask(Thread* thread);
  76 
  77   static bool is_initial_thread(void);
  78   static pid_t gettid();
  79 
  80   static int page_size(void)                                        { return _page_size; }
  81   static void set_page_size(int val)                                { _page_size = val; }
  82 
  83   static address   ucontext_get_pc(const ucontext_t* uc);
  84   static void ucontext_set_pc(ucontext_t* uc, address pc);
  85   static intptr_t* ucontext_get_sp(const ucontext_t* uc);
  86   static intptr_t* ucontext_get_fp(const ucontext_t* uc);
  87 
  88   // For Analyzer Forte AsyncGetCallTrace profiling support:
  89   //
  90   // This interface should be declared in os_bsd_i486.hpp, but
  91   // that file provides extensions to the os class and not the
  92   // Bsd class.
  93   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, const ucontext_t* uc,
  94                                               intptr_t** ret_sp, intptr_t** ret_fp);
  95 
  96   static bool get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr);
  97 
  98   // This boolean allows users to forward their own non-matching signals
  99   // to JVM_handle_bsd_signal, harmlessly.
 100   static bool signal_handlers_are_installed;
 101 
 102   static int get_our_sigflags(int);
 103   static void set_our_sigflags(int, int);
 104   static void signal_sets_init();
 105   static void install_signal_handlers();
 106   static void set_signal_handler(int, bool);
 107   static bool is_sig_ignored(int sig);
 108 
 109   static sigset_t* unblocked_signals();
 110   static sigset_t* vm_signals();
 111 
 112   // For signal-chaining
 113   static struct sigaction *get_chained_signal_action(int sig);
 114   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 115 
 116   // Real-time clock functions
 117   static void clock_init(void);
 118 
 119   // Stack repair handling
 120 
 121   // none present
 122 
 123  private:
 124   typedef int (*sched_getcpu_func_t)(void);
 125   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 126   typedef int (*numa_max_node_func_t)(void);
 127   typedef int (*numa_available_func_t)(void);
 128   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 129   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 130 
 131   static sched_getcpu_func_t _sched_getcpu;
 132   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 133   static numa_max_node_func_t _numa_max_node;
 134   static numa_available_func_t _numa_available;
 135   static numa_tonode_memory_func_t _numa_tonode_memory;
 136   static numa_interleave_memory_func_t _numa_interleave_memory;
 137   static unsigned long* _numa_all_nodes;
 138 
 139   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 140   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 141   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 142   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 143   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 144   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 145   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 146  public:
 147   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 148   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 149     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 150   }
 151   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 152   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 153   static int numa_tonode_memory(void *start, size_t size, int node) {
 154     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 155   }
 156   static void numa_interleave_memory(void *start, size_t size) {
 157     if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 158       _numa_interleave_memory(start, size, _numa_all_nodes);
 159     }
 160   }
 161   static int get_node_by_cpu(int cpu_id);
 162 };
 163 
 164 #endif // OS_BSD_VM_OS_BSD_HPP