1 /*
   2  * Copyright (c) 1999, 2015, 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 #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 #ifdef __APPLE__
  59   // mach_absolute_time
  60   static mach_timebase_info_data_t _timebase_info;
  61   static volatile uint64_t         _max_abstime;
  62 #else
  63   static int (*_clock_gettime)(clockid_t, struct timespec *);
  64 #endif
  65 
  66   static GrowableArray<int>* _cpu_to_node;
  67 
  68  protected:
  69 
  70   static julong _physical_memory;
  71   static pthread_t _main_thread;
  72   static int _page_size;
  73 
  74   static julong available_memory();
  75   static julong physical_memory() { return _physical_memory; }
  76   static void initialize_system_info();
  77 
  78   static void rebuild_cpu_to_node_map();
  79   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
  80 
  81   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
  82 
  83  public:
  84 
  85   static void init_thread_fpu_state();
  86   static pthread_t main_thread(void)                                { return _main_thread; }
  87 
  88   static void hotspot_sigmask(Thread* thread);
  89 
  90   static bool is_initial_thread(void);
  91   static pid_t gettid();
  92 
  93   static int page_size(void)                                        { return _page_size; }
  94   static void set_page_size(int val)                                { _page_size = val; }
  95 
  96   static address   ucontext_get_pc(ucontext_t* uc);
  97   static void ucontext_set_pc(ucontext_t* uc, address pc);
  98   static intptr_t* ucontext_get_sp(ucontext_t* uc);
  99   static intptr_t* ucontext_get_fp(ucontext_t* uc);
 100 
 101   // For Analyzer Forte AsyncGetCallTrace profiling support:
 102   //
 103   // This interface should be declared in os_bsd_i486.hpp, but
 104   // that file provides extensions to the os class and not the
 105   // Bsd class.
 106   static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
 107                                               intptr_t** ret_sp, intptr_t** ret_fp);
 108 
 109    static bool get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr);
 110   
 111   // This boolean allows users to forward their own non-matching signals
 112   // to JVM_handle_bsd_signal, harmlessly.
 113   static bool signal_handlers_are_installed;
 114 
 115   static int get_our_sigflags(int);
 116   static void set_our_sigflags(int, int);
 117   static void signal_sets_init();
 118   static void install_signal_handlers();
 119   static void set_signal_handler(int, bool);
 120   static bool is_sig_ignored(int sig);
 121 
 122   static sigset_t* unblocked_signals();
 123   static sigset_t* vm_signals();
 124   static sigset_t* allowdebug_blocked_signals();
 125 
 126   // For signal-chaining
 127   static struct sigaction *get_chained_signal_action(int sig);
 128   static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
 129 
 130   // Minimum stack size a thread can be created with (allowing
 131   // the VM to completely create the thread and enter user code)
 132   static size_t min_stack_allowed;
 133 
 134   // Return default stack size or guard size for the specified thread type
 135   static size_t default_stack_size(os::ThreadType thr_type);
 136   static size_t default_guard_size(os::ThreadType thr_type);
 137 
 138   // Real-time clock functions
 139   static void clock_init(void);
 140 
 141   // Stack repair handling
 142 
 143   // none present
 144 
 145  private:
 146   typedef int (*sched_getcpu_func_t)(void);
 147   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
 148   typedef int (*numa_max_node_func_t)(void);
 149   typedef int (*numa_available_func_t)(void);
 150   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
 151   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
 152 
 153   static sched_getcpu_func_t _sched_getcpu;
 154   static numa_node_to_cpus_func_t _numa_node_to_cpus;
 155   static numa_max_node_func_t _numa_max_node;
 156   static numa_available_func_t _numa_available;
 157   static numa_tonode_memory_func_t _numa_tonode_memory;
 158   static numa_interleave_memory_func_t _numa_interleave_memory;
 159   static unsigned long* _numa_all_nodes;
 160 
 161   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
 162   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
 163   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
 164   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
 165   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
 166   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
 167   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
 168  public:
 169   static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
 170   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
 171     return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
 172   }
 173   static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
 174   static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
 175   static int numa_tonode_memory(void *start, size_t size, int node) {
 176     return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
 177   }
 178   static void numa_interleave_memory(void *start, size_t size) {
 179     if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
 180       _numa_interleave_memory(start, size, _numa_all_nodes);
 181     }
 182   }
 183   static int get_node_by_cpu(int cpu_id);
 184 };
 185 
 186 
 187 class PlatformEvent : public CHeapObj<mtInternal> {
 188  private:
 189   double CachePad[4];   // increase odds that _mutex is sole occupant of cache line
 190   volatile int _Event;
 191   volatile int _nParked;
 192   pthread_mutex_t _mutex[1];
 193   pthread_cond_t  _cond[1];
 194   double PostPad[2];
 195   Thread * _Assoc;
 196 
 197  public:       // TODO-FIXME: make dtor private
 198   ~PlatformEvent() { guarantee(0, "invariant"); }
 199 
 200  public:
 201   PlatformEvent() {
 202     int status;
 203     status = pthread_cond_init(_cond, NULL);
 204     assert_status(status == 0, status, "cond_init");
 205     status = pthread_mutex_init(_mutex, NULL);
 206     assert_status(status == 0, status, "mutex_init");
 207     _Event   = 0;
 208     _nParked = 0;
 209     _Assoc   = NULL;
 210   }
 211 
 212   // Use caution with reset() and fired() -- they may require MEMBARs
 213   void reset() { _Event = 0; }
 214   int  fired() { return _Event; }
 215   void park();
 216   void unpark();
 217   int  park(jlong millis);
 218   void SetAssociation(Thread * a) { _Assoc = a; }
 219 };
 220 
 221 class PlatformParker : public CHeapObj<mtInternal> {
 222  protected:
 223   pthread_mutex_t _mutex[1];
 224   pthread_cond_t  _cond[1];
 225 
 226  public:       // TODO-FIXME: make dtor private
 227   ~PlatformParker() { guarantee(0, "invariant"); }
 228 
 229  public:
 230   PlatformParker() {
 231     int status;
 232     status = pthread_cond_init(_cond, NULL);
 233     assert_status(status == 0, status, "cond_init");
 234     status = pthread_mutex_init(_mutex, NULL);
 235     assert_status(status == 0, status, "mutex_init");
 236   }
 237 };
 238 
 239 #endif // OS_BSD_VM_OS_BSD_HPP