src/os/linux/vm/os_linux.hpp

Thu, 21 Mar 2013 20:46:46 -0700

author
iklam
date
Thu, 21 Mar 2013 20:46:46 -0700
changeset 4812
14509df4cd63
parent 4710
9058789475af
child 4996
b4081e9714ec
permissions
-rw-r--r--

8010389: After fix for 7107135 a failed dlopen() call results in a VM crash
Summary: Call dlerror() in VM thread as necessary.
Reviewed-by: coleenp, dholmes

duke@435 1 /*
simonis@4675 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef OS_LINUX_VM_OS_LINUX_HPP
stefank@2314 26 #define OS_LINUX_VM_OS_LINUX_HPP
stefank@2314 27
duke@435 28 // Linux_OS defines the interface to Linux operating systems
duke@435 29
duke@435 30 /* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
duke@435 31 typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
duke@435 32
duke@435 33 class Linux {
duke@435 34 friend class os;
duke@435 35
duke@435 36 // For signal-chaining
duke@435 37 #define MAXSIGNUM 32
duke@435 38 static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
duke@435 39 static unsigned int sigs; // mask of signals that have
duke@435 40 // preinstalled signal handlers
duke@435 41 static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
duke@435 42 // __sigaction(), signal() is loaded
duke@435 43 static struct sigaction *(*get_signal_action)(int);
duke@435 44 static struct sigaction *get_preinstalled_handler(int);
duke@435 45 static void save_preinstalled_handler(int, struct sigaction&);
duke@435 46
duke@435 47 static void check_signal_handler(int sig);
duke@435 48
duke@435 49 // For signal flags diagnostics
duke@435 50 static int sigflags[MAXSIGNUM];
duke@435 51
duke@435 52 static int (*_clock_gettime)(clockid_t, struct timespec *);
duke@435 53 static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
duke@435 54
duke@435 55 static address _initial_thread_stack_bottom;
duke@435 56 static uintptr_t _initial_thread_stack_size;
duke@435 57
xlu@634 58 static const char *_glibc_version;
xlu@634 59 static const char *_libpthread_version;
duke@435 60
duke@435 61 static bool _is_floating_stack;
duke@435 62 static bool _is_NPTL;
duke@435 63 static bool _supports_fast_thread_cpu_time;
duke@435 64
iveresov@576 65 static GrowableArray<int>* _cpu_to_node;
iveresov@576 66
duke@435 67 protected:
duke@435 68
duke@435 69 static julong _physical_memory;
duke@435 70 static pthread_t _main_thread;
duke@435 71 static Mutex* _createThread_lock;
duke@435 72 static int _page_size;
duke@435 73
duke@435 74 static julong available_memory();
duke@435 75 static julong physical_memory() { return _physical_memory; }
duke@435 76 static void initialize_system_info();
duke@435 77
xlu@634 78 static void set_glibc_version(const char *s) { _glibc_version = s; }
xlu@634 79 static void set_libpthread_version(const char *s) { _libpthread_version = s; }
duke@435 80
duke@435 81 static bool supports_variable_stack_size();
duke@435 82
duke@435 83 static void set_is_NPTL() { _is_NPTL = true; }
duke@435 84 static void set_is_LinuxThreads() { _is_NPTL = false; }
duke@435 85 static void set_is_floating_stack() { _is_floating_stack = true; }
duke@435 86
iveresov@576 87 static void rebuild_cpu_to_node_map();
iveresov@576 88 static GrowableArray<int>* cpu_to_node() { return _cpu_to_node; }
iveresov@2818 89
iveresov@2818 90 static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
iveresov@2818 91
nloodin@3783 92 static void print_full_memory_info(outputStream* st);
nloodin@3783 93 static void print_distro_info(outputStream* st);
nloodin@3783 94 static void print_libversion_info(outputStream* st);
nloodin@3783 95
duke@435 96 public:
iklam@4710 97 static bool _stack_is_executable;
iklam@4812 98 static void *dlopen_helper(const char *name, char *ebuf, int ebuflen);
iklam@4812 99 static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen);
iklam@4710 100
duke@435 101 static void init_thread_fpu_state();
duke@435 102 static int get_fpu_control_word();
duke@435 103 static void set_fpu_control_word(int fpu_control);
duke@435 104 static pthread_t main_thread(void) { return _main_thread; }
duke@435 105 // returns kernel thread id (similar to LWP id on Solaris), which can be
duke@435 106 // used to access /proc
duke@435 107 static pid_t gettid();
duke@435 108 static void set_createThread_lock(Mutex* lk) { _createThread_lock = lk; }
duke@435 109 static Mutex* createThread_lock(void) { return _createThread_lock; }
duke@435 110 static void hotspot_sigmask(Thread* thread);
duke@435 111
duke@435 112 static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; }
duke@435 113 static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; }
duke@435 114 static bool is_initial_thread(void);
duke@435 115
duke@435 116 static int page_size(void) { return _page_size; }
duke@435 117 static void set_page_size(int val) { _page_size = val; }
duke@435 118
duke@435 119 static address ucontext_get_pc(ucontext_t* uc);
duke@435 120 static intptr_t* ucontext_get_sp(ucontext_t* uc);
duke@435 121 static intptr_t* ucontext_get_fp(ucontext_t* uc);
duke@435 122
duke@435 123 // For Analyzer Forte AsyncGetCallTrace profiling support:
duke@435 124 //
duke@435 125 // This interface should be declared in os_linux_i486.hpp, but
duke@435 126 // that file provides extensions to the os class and not the
duke@435 127 // Linux class.
duke@435 128 static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
duke@435 129 intptr_t** ret_sp, intptr_t** ret_fp);
duke@435 130
duke@435 131 // This boolean allows users to forward their own non-matching signals
duke@435 132 // to JVM_handle_linux_signal, harmlessly.
duke@435 133 static bool signal_handlers_are_installed;
duke@435 134
duke@435 135 static int get_our_sigflags(int);
duke@435 136 static void set_our_sigflags(int, int);
duke@435 137 static void signal_sets_init();
duke@435 138 static void install_signal_handlers();
duke@435 139 static void set_signal_handler(int, bool);
duke@435 140 static bool is_sig_ignored(int sig);
duke@435 141
duke@435 142 static sigset_t* unblocked_signals();
duke@435 143 static sigset_t* vm_signals();
duke@435 144 static sigset_t* allowdebug_blocked_signals();
duke@435 145
duke@435 146 // For signal-chaining
duke@435 147 static struct sigaction *get_chained_signal_action(int sig);
duke@435 148 static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
duke@435 149
duke@435 150 // GNU libc and libpthread version strings
xlu@634 151 static const char *glibc_version() { return _glibc_version; }
xlu@634 152 static const char *libpthread_version() { return _libpthread_version; }
duke@435 153
duke@435 154 // NPTL or LinuxThreads?
duke@435 155 static bool is_LinuxThreads() { return !_is_NPTL; }
duke@435 156 static bool is_NPTL() { return _is_NPTL; }
duke@435 157
duke@435 158 // NPTL is always floating stack. LinuxThreads could be using floating
duke@435 159 // stack or fixed stack.
duke@435 160 static bool is_floating_stack() { return _is_floating_stack; }
duke@435 161
duke@435 162 static void libpthread_init();
iveresov@897 163 static bool libnuma_init();
iveresov@1198 164 static void* libnuma_dlsym(void* handle, const char* name);
duke@435 165 // Minimum stack size a thread can be created with (allowing
duke@435 166 // the VM to completely create the thread and enter user code)
duke@435 167 static size_t min_stack_allowed;
duke@435 168
duke@435 169 // Return default stack size or guard size for the specified thread type
duke@435 170 static size_t default_stack_size(os::ThreadType thr_type);
duke@435 171 static size_t default_guard_size(os::ThreadType thr_type);
duke@435 172
duke@435 173 static void capture_initial_stack(size_t max_size);
duke@435 174
duke@435 175 // Stack overflow handling
duke@435 176 static bool manually_expand_stack(JavaThread * t, address addr);
duke@435 177 static int max_register_window_saves_before_flushing();
duke@435 178
duke@435 179 // Real-time clock functions
duke@435 180 static void clock_init(void);
duke@435 181
duke@435 182 // fast POSIX clocks support
duke@435 183 static void fast_thread_clock_init(void);
duke@435 184
johnc@4017 185 static inline bool supports_monotonic_clock() {
duke@435 186 return _clock_gettime != NULL;
duke@435 187 }
duke@435 188
duke@435 189 static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
duke@435 190 return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
duke@435 191 }
duke@435 192
duke@435 193 static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
duke@435 194 return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
duke@435 195 }
duke@435 196
duke@435 197 static bool supports_fast_thread_cpu_time() {
duke@435 198 return _supports_fast_thread_cpu_time;
duke@435 199 }
duke@435 200
duke@435 201 static jlong fast_thread_cpu_time(clockid_t clockid);
duke@435 202
duke@435 203 // Stack repair handling
duke@435 204
duke@435 205 // none present
duke@435 206
duke@435 207 // LinuxThreads work-around for 6292965
duke@435 208 static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
duke@435 209
duke@435 210
duke@435 211 // Linux suspend/resume support - this helper is a shadow of its former
duke@435 212 // self now that low-level suspension is barely used, and old workarounds
duke@435 213 // for LinuxThreads are no longer needed.
duke@435 214 class SuspendResume {
duke@435 215 private:
simonis@4675 216 volatile int _suspend_action;
simonis@4675 217 volatile jint _state;
simonis@4675 218 public:
duke@435 219 // values for suspend_action:
simonis@4675 220 enum {
simonis@4675 221 SR_NONE = 0x00,
simonis@4675 222 SR_SUSPEND = 0x01, // suspend request
simonis@4675 223 SR_CONTINUE = 0x02, // resume request
simonis@4675 224 SR_SUSPENDED = 0x20 // values for _state: + SR_NONE
simonis@4675 225 };
duke@435 226
duke@435 227 SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
duke@435 228
duke@435 229 int suspend_action() const { return _suspend_action; }
duke@435 230 void set_suspend_action(int x) { _suspend_action = x; }
duke@435 231
duke@435 232 // atomic updates for _state
simonis@4675 233 inline void set_suspended();
simonis@4675 234 inline void clear_suspended();
duke@435 235 bool is_suspended() { return _state & SR_SUSPENDED; }
duke@435 236
duke@435 237 };
iveresov@576 238
iveresov@576 239 private:
iveresov@576 240 typedef int (*sched_getcpu_func_t)(void);
iveresov@576 241 typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
iveresov@576 242 typedef int (*numa_max_node_func_t)(void);
iveresov@576 243 typedef int (*numa_available_func_t)(void);
iveresov@576 244 typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
iveresov@897 245 typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
iveresov@576 246
iveresov@576 247 static sched_getcpu_func_t _sched_getcpu;
iveresov@576 248 static numa_node_to_cpus_func_t _numa_node_to_cpus;
iveresov@576 249 static numa_max_node_func_t _numa_max_node;
iveresov@576 250 static numa_available_func_t _numa_available;
iveresov@576 251 static numa_tonode_memory_func_t _numa_tonode_memory;
iveresov@897 252 static numa_interleave_memory_func_t _numa_interleave_memory;
iveresov@897 253 static unsigned long* _numa_all_nodes;
iveresov@576 254
iveresov@576 255 static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
iveresov@576 256 static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
iveresov@576 257 static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
iveresov@576 258 static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
iveresov@576 259 static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
iveresov@897 260 static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
iveresov@897 261 static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
iveresov@3024 262 static int sched_getcpu_syscall(void);
iveresov@576 263 public:
iveresov@576 264 static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
iveresov@576 265 static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
iveresov@576 266 return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
iveresov@576 267 }
iveresov@576 268 static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
iveresov@576 269 static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
iveresov@576 270 static int numa_tonode_memory(void *start, size_t size, int node) {
iveresov@576 271 return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
iveresov@576 272 }
iveresov@897 273 static void numa_interleave_memory(void *start, size_t size) {
iveresov@897 274 if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
iveresov@897 275 _numa_interleave_memory(start, size, _numa_all_nodes);
iveresov@897 276 }
iveresov@897 277 }
iveresov@576 278 static int get_node_by_cpu(int cpu_id);
duke@435 279 };
duke@435 280
duke@435 281
zgu@3900 282 class PlatformEvent : public CHeapObj<mtInternal> {
duke@435 283 private:
duke@435 284 double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line
duke@435 285 volatile int _Event ;
duke@435 286 volatile int _nParked ;
duke@435 287 pthread_mutex_t _mutex [1] ;
duke@435 288 pthread_cond_t _cond [1] ;
duke@435 289 double PostPad [2] ;
duke@435 290 Thread * _Assoc ;
duke@435 291
duke@435 292 public: // TODO-FIXME: make dtor private
duke@435 293 ~PlatformEvent() { guarantee (0, "invariant") ; }
duke@435 294
duke@435 295 public:
duke@435 296 PlatformEvent() {
duke@435 297 int status;
duke@435 298 status = pthread_cond_init (_cond, NULL);
duke@435 299 assert_status(status == 0, status, "cond_init");
duke@435 300 status = pthread_mutex_init (_mutex, NULL);
duke@435 301 assert_status(status == 0, status, "mutex_init");
duke@435 302 _Event = 0 ;
duke@435 303 _nParked = 0 ;
duke@435 304 _Assoc = NULL ;
duke@435 305 }
duke@435 306
duke@435 307 // Use caution with reset() and fired() -- they may require MEMBARs
duke@435 308 void reset() { _Event = 0 ; }
duke@435 309 int fired() { return _Event; }
duke@435 310 void park () ;
duke@435 311 void unpark () ;
duke@435 312 int TryPark () ;
duke@435 313 int park (jlong millis) ;
duke@435 314 void SetAssociation (Thread * a) { _Assoc = a ; }
duke@435 315 } ;
duke@435 316
zgu@3900 317 class PlatformParker : public CHeapObj<mtInternal> {
duke@435 318 protected:
duke@435 319 pthread_mutex_t _mutex [1] ;
duke@435 320 pthread_cond_t _cond [1] ;
duke@435 321
duke@435 322 public: // TODO-FIXME: make dtor private
duke@435 323 ~PlatformParker() { guarantee (0, "invariant") ; }
duke@435 324
duke@435 325 public:
duke@435 326 PlatformParker() {
duke@435 327 int status;
duke@435 328 status = pthread_cond_init (_cond, NULL);
duke@435 329 assert_status(status == 0, status, "cond_init");
duke@435 330 status = pthread_mutex_init (_mutex, NULL);
duke@435 331 assert_status(status == 0, status, "mutex_init");
duke@435 332 }
duke@435 333 } ;
stefank@2314 334
stefank@2314 335 #endif // OS_LINUX_VM_OS_LINUX_HPP

mercurial