duke@435: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef OS_LINUX_VM_OS_LINUX_HPP stefank@2314: #define OS_LINUX_VM_OS_LINUX_HPP stefank@2314: duke@435: // Linux_OS defines the interface to Linux operating systems duke@435: duke@435: /* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */ duke@435: typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *); duke@435: duke@435: class Linux { duke@435: friend class os; duke@435: duke@435: // For signal-chaining duke@435: #define MAXSIGNUM 32 duke@435: static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions duke@435: static unsigned int sigs; // mask of signals that have duke@435: // preinstalled signal handlers duke@435: static bool libjsig_is_loaded; // libjsig that interposes sigaction(), duke@435: // __sigaction(), signal() is loaded duke@435: static struct sigaction *(*get_signal_action)(int); duke@435: static struct sigaction *get_preinstalled_handler(int); duke@435: static void save_preinstalled_handler(int, struct sigaction&); duke@435: duke@435: static void check_signal_handler(int sig); duke@435: duke@435: // For signal flags diagnostics duke@435: static int sigflags[MAXSIGNUM]; duke@435: duke@435: static int (*_clock_gettime)(clockid_t, struct timespec *); duke@435: static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *); duke@435: duke@435: static address _initial_thread_stack_bottom; duke@435: static uintptr_t _initial_thread_stack_size; duke@435: xlu@634: static const char *_glibc_version; xlu@634: static const char *_libpthread_version; duke@435: duke@435: static bool _is_floating_stack; duke@435: static bool _is_NPTL; duke@435: static bool _supports_fast_thread_cpu_time; duke@435: iveresov@576: static GrowableArray* _cpu_to_node; iveresov@576: duke@435: protected: duke@435: duke@435: static julong _physical_memory; duke@435: static pthread_t _main_thread; duke@435: static Mutex* _createThread_lock; duke@435: static int _page_size; duke@435: duke@435: static julong available_memory(); duke@435: static julong physical_memory() { return _physical_memory; } duke@435: static void initialize_system_info(); duke@435: xlu@634: static void set_glibc_version(const char *s) { _glibc_version = s; } xlu@634: static void set_libpthread_version(const char *s) { _libpthread_version = s; } duke@435: duke@435: static bool supports_variable_stack_size(); duke@435: duke@435: static void set_is_NPTL() { _is_NPTL = true; } duke@435: static void set_is_LinuxThreads() { _is_NPTL = false; } duke@435: static void set_is_floating_stack() { _is_floating_stack = true; } duke@435: iveresov@576: static void rebuild_cpu_to_node_map(); iveresov@576: static GrowableArray* cpu_to_node() { return _cpu_to_node; } duke@435: public: duke@435: static void init_thread_fpu_state(); duke@435: static int get_fpu_control_word(); duke@435: static void set_fpu_control_word(int fpu_control); duke@435: static pthread_t main_thread(void) { return _main_thread; } duke@435: // returns kernel thread id (similar to LWP id on Solaris), which can be duke@435: // used to access /proc duke@435: static pid_t gettid(); duke@435: static void set_createThread_lock(Mutex* lk) { _createThread_lock = lk; } duke@435: static Mutex* createThread_lock(void) { return _createThread_lock; } duke@435: static void hotspot_sigmask(Thread* thread); duke@435: duke@435: static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; } duke@435: static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; } duke@435: static bool is_initial_thread(void); duke@435: duke@435: static int page_size(void) { return _page_size; } duke@435: static void set_page_size(int val) { _page_size = val; } duke@435: duke@435: static address ucontext_get_pc(ucontext_t* uc); duke@435: static intptr_t* ucontext_get_sp(ucontext_t* uc); duke@435: static intptr_t* ucontext_get_fp(ucontext_t* uc); duke@435: duke@435: // For Analyzer Forte AsyncGetCallTrace profiling support: duke@435: // duke@435: // This interface should be declared in os_linux_i486.hpp, but duke@435: // that file provides extensions to the os class and not the duke@435: // Linux class. duke@435: static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc, duke@435: intptr_t** ret_sp, intptr_t** ret_fp); duke@435: duke@435: // This boolean allows users to forward their own non-matching signals duke@435: // to JVM_handle_linux_signal, harmlessly. duke@435: static bool signal_handlers_are_installed; duke@435: duke@435: static int get_our_sigflags(int); duke@435: static void set_our_sigflags(int, int); duke@435: static void signal_sets_init(); duke@435: static void install_signal_handlers(); duke@435: static void set_signal_handler(int, bool); duke@435: static bool is_sig_ignored(int sig); duke@435: duke@435: static sigset_t* unblocked_signals(); duke@435: static sigset_t* vm_signals(); duke@435: static sigset_t* allowdebug_blocked_signals(); duke@435: duke@435: // For signal-chaining duke@435: static struct sigaction *get_chained_signal_action(int sig); duke@435: static bool chained_handler(int sig, siginfo_t* siginfo, void* context); duke@435: duke@435: // GNU libc and libpthread version strings xlu@634: static const char *glibc_version() { return _glibc_version; } xlu@634: static const char *libpthread_version() { return _libpthread_version; } duke@435: duke@435: // NPTL or LinuxThreads? duke@435: static bool is_LinuxThreads() { return !_is_NPTL; } duke@435: static bool is_NPTL() { return _is_NPTL; } duke@435: duke@435: // NPTL is always floating stack. LinuxThreads could be using floating duke@435: // stack or fixed stack. duke@435: static bool is_floating_stack() { return _is_floating_stack; } duke@435: duke@435: static void libpthread_init(); iveresov@897: static bool libnuma_init(); iveresov@1198: static void* libnuma_dlsym(void* handle, const char* name); duke@435: // Minimum stack size a thread can be created with (allowing duke@435: // the VM to completely create the thread and enter user code) duke@435: static size_t min_stack_allowed; duke@435: duke@435: // Return default stack size or guard size for the specified thread type duke@435: static size_t default_stack_size(os::ThreadType thr_type); duke@435: static size_t default_guard_size(os::ThreadType thr_type); duke@435: duke@435: static void capture_initial_stack(size_t max_size); duke@435: duke@435: // Stack overflow handling duke@435: static bool manually_expand_stack(JavaThread * t, address addr); duke@435: static int max_register_window_saves_before_flushing(); duke@435: duke@435: // Real-time clock functions duke@435: static void clock_init(void); duke@435: duke@435: // fast POSIX clocks support duke@435: static void fast_thread_clock_init(void); duke@435: duke@435: static bool supports_monotonic_clock() { duke@435: return _clock_gettime != NULL; duke@435: } duke@435: duke@435: static int clock_gettime(clockid_t clock_id, struct timespec *tp) { duke@435: return _clock_gettime ? _clock_gettime(clock_id, tp) : -1; duke@435: } duke@435: duke@435: static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) { duke@435: return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1; duke@435: } duke@435: duke@435: static bool supports_fast_thread_cpu_time() { duke@435: return _supports_fast_thread_cpu_time; duke@435: } duke@435: duke@435: static jlong fast_thread_cpu_time(clockid_t clockid); duke@435: duke@435: // Stack repair handling duke@435: duke@435: // none present duke@435: duke@435: // LinuxThreads work-around for 6292965 duke@435: static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime); duke@435: duke@435: duke@435: // Linux suspend/resume support - this helper is a shadow of its former duke@435: // self now that low-level suspension is barely used, and old workarounds duke@435: // for LinuxThreads are no longer needed. duke@435: class SuspendResume { duke@435: private: duke@435: volatile int _suspend_action; duke@435: // values for suspend_action: duke@435: #define SR_NONE (0x00) duke@435: #define SR_SUSPEND (0x01) // suspend request duke@435: #define SR_CONTINUE (0x02) // resume request duke@435: duke@435: volatile jint _state; duke@435: // values for _state: + SR_NONE duke@435: #define SR_SUSPENDED (0x20) duke@435: public: duke@435: SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; } duke@435: duke@435: int suspend_action() const { return _suspend_action; } duke@435: void set_suspend_action(int x) { _suspend_action = x; } duke@435: duke@435: // atomic updates for _state duke@435: void set_suspended() { duke@435: jint temp, temp2; duke@435: do { duke@435: temp = _state; duke@435: temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp); duke@435: } while (temp2 != temp); duke@435: } duke@435: void clear_suspended() { duke@435: jint temp, temp2; duke@435: do { duke@435: temp = _state; duke@435: temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp); duke@435: } while (temp2 != temp); duke@435: } duke@435: bool is_suspended() { return _state & SR_SUSPENDED; } duke@435: duke@435: #undef SR_SUSPENDED duke@435: }; iveresov@576: iveresov@576: private: iveresov@576: typedef int (*sched_getcpu_func_t)(void); iveresov@576: typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen); iveresov@576: typedef int (*numa_max_node_func_t)(void); iveresov@576: typedef int (*numa_available_func_t)(void); iveresov@576: typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node); iveresov@897: typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask); iveresov@576: iveresov@576: static sched_getcpu_func_t _sched_getcpu; iveresov@576: static numa_node_to_cpus_func_t _numa_node_to_cpus; iveresov@576: static numa_max_node_func_t _numa_max_node; iveresov@576: static numa_available_func_t _numa_available; iveresov@576: static numa_tonode_memory_func_t _numa_tonode_memory; iveresov@897: static numa_interleave_memory_func_t _numa_interleave_memory; iveresov@897: static unsigned long* _numa_all_nodes; iveresov@576: iveresov@576: static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; } iveresov@576: static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; } iveresov@576: static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; } iveresov@576: static void set_numa_available(numa_available_func_t func) { _numa_available = func; } iveresov@576: static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; } iveresov@897: static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; } iveresov@897: static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; } iveresov@576: public: iveresov@576: static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; } iveresov@576: static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) { iveresov@576: return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1; iveresov@576: } iveresov@576: static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; } iveresov@576: static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; } iveresov@576: static int numa_tonode_memory(void *start, size_t size, int node) { iveresov@576: return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1; iveresov@576: } iveresov@897: static void numa_interleave_memory(void *start, size_t size) { iveresov@897: if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) { iveresov@897: _numa_interleave_memory(start, size, _numa_all_nodes); iveresov@897: } iveresov@897: } iveresov@576: static int get_node_by_cpu(int cpu_id); duke@435: }; duke@435: duke@435: duke@435: class PlatformEvent : public CHeapObj { duke@435: private: duke@435: double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line duke@435: volatile int _Event ; duke@435: volatile int _nParked ; duke@435: pthread_mutex_t _mutex [1] ; duke@435: pthread_cond_t _cond [1] ; duke@435: double PostPad [2] ; duke@435: Thread * _Assoc ; duke@435: duke@435: public: // TODO-FIXME: make dtor private duke@435: ~PlatformEvent() { guarantee (0, "invariant") ; } duke@435: duke@435: public: duke@435: PlatformEvent() { duke@435: int status; duke@435: status = pthread_cond_init (_cond, NULL); duke@435: assert_status(status == 0, status, "cond_init"); duke@435: status = pthread_mutex_init (_mutex, NULL); duke@435: assert_status(status == 0, status, "mutex_init"); duke@435: _Event = 0 ; duke@435: _nParked = 0 ; duke@435: _Assoc = NULL ; duke@435: } duke@435: duke@435: // Use caution with reset() and fired() -- they may require MEMBARs duke@435: void reset() { _Event = 0 ; } duke@435: int fired() { return _Event; } duke@435: void park () ; duke@435: void unpark () ; duke@435: int TryPark () ; duke@435: int park (jlong millis) ; duke@435: void SetAssociation (Thread * a) { _Assoc = a ; } duke@435: } ; duke@435: duke@435: class PlatformParker : public CHeapObj { duke@435: protected: duke@435: pthread_mutex_t _mutex [1] ; duke@435: pthread_cond_t _cond [1] ; duke@435: duke@435: public: // TODO-FIXME: make dtor private duke@435: ~PlatformParker() { guarantee (0, "invariant") ; } duke@435: duke@435: public: duke@435: PlatformParker() { duke@435: int status; duke@435: status = pthread_cond_init (_cond, NULL); duke@435: assert_status(status == 0, status, "cond_init"); duke@435: status = pthread_mutex_init (_mutex, NULL); duke@435: assert_status(status == 0, status, "mutex_init"); duke@435: } duke@435: } ; stefank@2314: stefank@2314: #endif // OS_LINUX_VM_OS_LINUX_HPP