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