Tue, 08 Aug 2017 15:57:29 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef OS_BSD_VM_OS_BSD_HPP |
aoqi@0 | 26 | #define OS_BSD_VM_OS_BSD_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | // Bsd_OS defines the interface to Bsd operating systems |
aoqi@0 | 29 | |
aoqi@0 | 30 | // Information about the protection of the page at address '0' on this os. |
aoqi@0 | 31 | static bool zero_page_read_protected() { return true; } |
aoqi@0 | 32 | |
aoqi@0 | 33 | /* pthread_getattr_np comes with BsdThreads-0.9-7 on RedHat 7.1 */ |
aoqi@0 | 34 | typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *); |
aoqi@0 | 35 | |
aoqi@0 | 36 | #ifdef __APPLE__ |
aoqi@0 | 37 | // Mac OS X doesn't support clock_gettime. Stub out the type, it is |
aoqi@0 | 38 | // unused |
aoqi@0 | 39 | typedef int clockid_t; |
aoqi@0 | 40 | #endif |
aoqi@0 | 41 | |
aoqi@0 | 42 | class Bsd { |
aoqi@0 | 43 | friend class os; |
aoqi@0 | 44 | |
aoqi@0 | 45 | // For signal-chaining |
aoqi@0 | 46 | #define MAXSIGNUM 32 |
aoqi@0 | 47 | static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions |
aoqi@0 | 48 | static unsigned int sigs; // mask of signals that have |
aoqi@0 | 49 | // preinstalled signal handlers |
aoqi@0 | 50 | static bool libjsig_is_loaded; // libjsig that interposes sigaction(), |
aoqi@0 | 51 | // __sigaction(), signal() is loaded |
aoqi@0 | 52 | static struct sigaction *(*get_signal_action)(int); |
aoqi@0 | 53 | static struct sigaction *get_preinstalled_handler(int); |
aoqi@0 | 54 | static void save_preinstalled_handler(int, struct sigaction&); |
aoqi@0 | 55 | |
aoqi@0 | 56 | static void check_signal_handler(int sig); |
aoqi@0 | 57 | |
aoqi@0 | 58 | // For signal flags diagnostics |
aoqi@0 | 59 | static int sigflags[MAXSIGNUM]; |
aoqi@0 | 60 | |
aoqi@0 | 61 | #ifdef __APPLE__ |
aoqi@0 | 62 | // mach_absolute_time |
aoqi@0 | 63 | static mach_timebase_info_data_t _timebase_info; |
aoqi@0 | 64 | static volatile uint64_t _max_abstime; |
aoqi@0 | 65 | #else |
aoqi@0 | 66 | static int (*_clock_gettime)(clockid_t, struct timespec *); |
aoqi@0 | 67 | #endif |
aoqi@0 | 68 | |
aoqi@0 | 69 | static GrowableArray<int>* _cpu_to_node; |
aoqi@0 | 70 | |
aoqi@0 | 71 | protected: |
aoqi@0 | 72 | |
aoqi@0 | 73 | static julong _physical_memory; |
aoqi@0 | 74 | static pthread_t _main_thread; |
aoqi@0 | 75 | static int _page_size; |
aoqi@0 | 76 | |
aoqi@0 | 77 | static julong available_memory(); |
aoqi@0 | 78 | static julong physical_memory() { return _physical_memory; } |
aoqi@0 | 79 | static void initialize_system_info(); |
aoqi@0 | 80 | |
aoqi@0 | 81 | static bool supports_variable_stack_size(); |
aoqi@0 | 82 | |
aoqi@0 | 83 | static void rebuild_cpu_to_node_map(); |
aoqi@0 | 84 | static GrowableArray<int>* cpu_to_node() { return _cpu_to_node; } |
aoqi@0 | 85 | |
aoqi@0 | 86 | static bool hugetlbfs_sanity_check(bool warn, size_t page_size); |
aoqi@0 | 87 | |
aoqi@0 | 88 | public: |
aoqi@0 | 89 | |
aoqi@0 | 90 | static void init_thread_fpu_state(); |
aoqi@0 | 91 | static pthread_t main_thread(void) { return _main_thread; } |
aoqi@0 | 92 | |
aoqi@0 | 93 | static void hotspot_sigmask(Thread* thread); |
aoqi@0 | 94 | |
aoqi@0 | 95 | static bool is_initial_thread(void); |
aoqi@0 | 96 | static pid_t gettid(); |
aoqi@0 | 97 | |
aoqi@0 | 98 | static int page_size(void) { return _page_size; } |
aoqi@0 | 99 | static void set_page_size(int val) { _page_size = val; } |
aoqi@0 | 100 | |
aoqi@0 | 101 | static address ucontext_get_pc(ucontext_t* uc); |
aoqi@0 | 102 | static intptr_t* ucontext_get_sp(ucontext_t* uc); |
aoqi@0 | 103 | static intptr_t* ucontext_get_fp(ucontext_t* uc); |
aoqi@0 | 104 | |
aoqi@0 | 105 | // For Analyzer Forte AsyncGetCallTrace profiling support: |
aoqi@0 | 106 | // |
aoqi@0 | 107 | // This interface should be declared in os_bsd_i486.hpp, but |
aoqi@0 | 108 | // that file provides extensions to the os class and not the |
aoqi@0 | 109 | // Bsd class. |
aoqi@0 | 110 | static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc, |
aoqi@0 | 111 | intptr_t** ret_sp, intptr_t** ret_fp); |
aoqi@0 | 112 | |
aoqi@0 | 113 | // This boolean allows users to forward their own non-matching signals |
aoqi@0 | 114 | // to JVM_handle_bsd_signal, harmlessly. |
aoqi@0 | 115 | static bool signal_handlers_are_installed; |
aoqi@0 | 116 | |
aoqi@0 | 117 | static int get_our_sigflags(int); |
aoqi@0 | 118 | static void set_our_sigflags(int, int); |
aoqi@0 | 119 | static void signal_sets_init(); |
aoqi@0 | 120 | static void install_signal_handlers(); |
aoqi@0 | 121 | static void set_signal_handler(int, bool); |
aoqi@0 | 122 | static bool is_sig_ignored(int sig); |
aoqi@0 | 123 | |
aoqi@0 | 124 | static sigset_t* unblocked_signals(); |
aoqi@0 | 125 | static sigset_t* vm_signals(); |
aoqi@0 | 126 | static sigset_t* allowdebug_blocked_signals(); |
aoqi@0 | 127 | |
aoqi@0 | 128 | // For signal-chaining |
aoqi@0 | 129 | static struct sigaction *get_chained_signal_action(int sig); |
aoqi@0 | 130 | static bool chained_handler(int sig, siginfo_t* siginfo, void* context); |
aoqi@0 | 131 | |
aoqi@0 | 132 | // Minimum stack size a thread can be created with (allowing |
aoqi@0 | 133 | // the VM to completely create the thread and enter user code) |
aoqi@0 | 134 | static size_t min_stack_allowed; |
aoqi@0 | 135 | |
aoqi@0 | 136 | // Return default stack size or guard size for the specified thread type |
aoqi@0 | 137 | static size_t default_stack_size(os::ThreadType thr_type); |
aoqi@0 | 138 | static size_t default_guard_size(os::ThreadType thr_type); |
aoqi@0 | 139 | |
aoqi@0 | 140 | // Real-time clock functions |
aoqi@0 | 141 | static void clock_init(void); |
aoqi@0 | 142 | |
aoqi@0 | 143 | static inline bool supports_monotonic_clock() { |
aoqi@0 | 144 | #ifdef __APPLE__ |
aoqi@0 | 145 | return true; |
aoqi@0 | 146 | #else |
aoqi@0 | 147 | return _clock_gettime != NULL; |
aoqi@0 | 148 | #endif |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | // Stack repair handling |
aoqi@0 | 152 | |
aoqi@0 | 153 | // none present |
aoqi@0 | 154 | |
aoqi@0 | 155 | // BsdThreads work-around for 6292965 |
aoqi@0 | 156 | static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime); |
aoqi@0 | 157 | |
aoqi@0 | 158 | private: |
aoqi@0 | 159 | typedef int (*sched_getcpu_func_t)(void); |
aoqi@0 | 160 | typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen); |
aoqi@0 | 161 | typedef int (*numa_max_node_func_t)(void); |
aoqi@0 | 162 | typedef int (*numa_available_func_t)(void); |
aoqi@0 | 163 | typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node); |
aoqi@0 | 164 | typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask); |
aoqi@0 | 165 | |
aoqi@0 | 166 | static sched_getcpu_func_t _sched_getcpu; |
aoqi@0 | 167 | static numa_node_to_cpus_func_t _numa_node_to_cpus; |
aoqi@0 | 168 | static numa_max_node_func_t _numa_max_node; |
aoqi@0 | 169 | static numa_available_func_t _numa_available; |
aoqi@0 | 170 | static numa_tonode_memory_func_t _numa_tonode_memory; |
aoqi@0 | 171 | static numa_interleave_memory_func_t _numa_interleave_memory; |
aoqi@0 | 172 | static unsigned long* _numa_all_nodes; |
aoqi@0 | 173 | |
aoqi@0 | 174 | static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; } |
aoqi@0 | 175 | static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; } |
aoqi@0 | 176 | static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; } |
aoqi@0 | 177 | static void set_numa_available(numa_available_func_t func) { _numa_available = func; } |
aoqi@0 | 178 | static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; } |
aoqi@0 | 179 | static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; } |
aoqi@0 | 180 | static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; } |
aoqi@0 | 181 | public: |
aoqi@0 | 182 | static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; } |
aoqi@0 | 183 | static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) { |
aoqi@0 | 184 | return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1; |
aoqi@0 | 185 | } |
aoqi@0 | 186 | static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; } |
aoqi@0 | 187 | static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; } |
aoqi@0 | 188 | static int numa_tonode_memory(void *start, size_t size, int node) { |
aoqi@0 | 189 | return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1; |
aoqi@0 | 190 | } |
aoqi@0 | 191 | static void numa_interleave_memory(void *start, size_t size) { |
aoqi@0 | 192 | if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) { |
aoqi@0 | 193 | _numa_interleave_memory(start, size, _numa_all_nodes); |
aoqi@0 | 194 | } |
aoqi@0 | 195 | } |
aoqi@0 | 196 | static int get_node_by_cpu(int cpu_id); |
aoqi@0 | 197 | }; |
aoqi@0 | 198 | |
aoqi@0 | 199 | |
aoqi@0 | 200 | class PlatformEvent : public CHeapObj<mtInternal> { |
aoqi@0 | 201 | private: |
aoqi@0 | 202 | double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line |
aoqi@0 | 203 | volatile int _Event ; |
aoqi@0 | 204 | volatile int _nParked ; |
aoqi@0 | 205 | pthread_mutex_t _mutex [1] ; |
aoqi@0 | 206 | pthread_cond_t _cond [1] ; |
aoqi@0 | 207 | double PostPad [2] ; |
aoqi@0 | 208 | Thread * _Assoc ; |
aoqi@0 | 209 | |
aoqi@0 | 210 | public: // TODO-FIXME: make dtor private |
aoqi@0 | 211 | ~PlatformEvent() { guarantee (0, "invariant") ; } |
aoqi@0 | 212 | |
aoqi@0 | 213 | public: |
aoqi@0 | 214 | PlatformEvent() { |
aoqi@0 | 215 | int status; |
aoqi@0 | 216 | status = pthread_cond_init (_cond, NULL); |
aoqi@0 | 217 | assert_status(status == 0, status, "cond_init"); |
aoqi@0 | 218 | status = pthread_mutex_init (_mutex, NULL); |
aoqi@0 | 219 | assert_status(status == 0, status, "mutex_init"); |
aoqi@0 | 220 | _Event = 0 ; |
aoqi@0 | 221 | _nParked = 0 ; |
aoqi@0 | 222 | _Assoc = NULL ; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | // Use caution with reset() and fired() -- they may require MEMBARs |
aoqi@0 | 226 | void reset() { _Event = 0 ; } |
aoqi@0 | 227 | int fired() { return _Event; } |
aoqi@0 | 228 | void park () ; |
aoqi@0 | 229 | void unpark () ; |
aoqi@0 | 230 | int TryPark () ; |
aoqi@0 | 231 | int park (jlong millis) ; |
aoqi@0 | 232 | void SetAssociation (Thread * a) { _Assoc = a ; } |
aoqi@0 | 233 | }; |
aoqi@0 | 234 | |
aoqi@0 | 235 | class PlatformParker : public CHeapObj<mtInternal> { |
aoqi@0 | 236 | protected: |
aoqi@0 | 237 | pthread_mutex_t _mutex [1] ; |
aoqi@0 | 238 | pthread_cond_t _cond [1] ; |
aoqi@0 | 239 | |
aoqi@0 | 240 | public: // TODO-FIXME: make dtor private |
aoqi@0 | 241 | ~PlatformParker() { guarantee (0, "invariant") ; } |
aoqi@0 | 242 | |
aoqi@0 | 243 | public: |
aoqi@0 | 244 | PlatformParker() { |
aoqi@0 | 245 | int status; |
aoqi@0 | 246 | status = pthread_cond_init (_cond, NULL); |
aoqi@0 | 247 | assert_status(status == 0, status, "cond_init"); |
aoqi@0 | 248 | status = pthread_mutex_init (_mutex, NULL); |
aoqi@0 | 249 | assert_status(status == 0, status, "mutex_init"); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | }; |
aoqi@0 | 252 | |
aoqi@0 | 253 | #endif // OS_BSD_VM_OS_BSD_HPP |