duke@435: /* simonis@4675: * Copyright (c) 1999, 2013, 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: goetz@6486: // Information about the protection of the page at address '0' on this os. goetz@6486: static bool zero_page_read_protected() { return true; } goetz@6486: duke@435: class Linux { duke@435: friend class os; stefank@5578: friend class TestReserveMemorySpecial; 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; vladidan@4996: static const int _vm_default_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: dcubed@5255: static int commit_memory_impl(char* addr, size_t bytes, bool exec); dcubed@5255: static int commit_memory_impl(char* addr, size_t bytes, dcubed@5255: size_t alignment_hint, bool exec); dcubed@5255: 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; } iveresov@2818: stefank@5578: static size_t find_large_page_size(); stefank@5578: static size_t setup_large_page_size(); stefank@5578: stefank@5578: static bool setup_large_page_type(size_t page_size); stefank@5578: static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size); iveresov@2818: static bool hugetlbfs_sanity_check(bool warn, size_t page_size); iveresov@2818: stefank@5578: static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec); stefank@5578: static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec); stefank@5578: static char* reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec); stefank@5578: static char* reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec); stefank@5578: stefank@5578: static bool release_memory_special_shm(char* base, size_t bytes); stefank@5578: static bool release_memory_special_huge_tlbfs(char* base, size_t bytes); stefank@5578: nloodin@3783: static void print_full_memory_info(outputStream* st); nloodin@3783: static void print_distro_info(outputStream* st); nloodin@3783: static void print_libversion_info(outputStream* st); nloodin@3783: duke@435: public: iklam@4710: static bool _stack_is_executable; iklam@4812: static void *dlopen_helper(const char *name, char *ebuf, int ebuflen); iklam@4812: static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen); iklam@4710: 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: vladidan@4996: static int vm_default_page_size(void) { return _vm_default_page_size; } vladidan@4996: 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: johnc@4017: static inline 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: dholmes@5679: // pthread_cond clock suppport dholmes@5679: private: dholmes@5679: static pthread_condattr_t _condattr[1]; dholmes@5679: dholmes@5679: public: dholmes@5679: static pthread_condattr_t* condAttr() { return _condattr; } dholmes@5679: 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: 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); mgerdin@5644: typedef void (*numa_set_bind_policy_func_t)(int policy); 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; mgerdin@5644: static numa_set_bind_policy_func_t _numa_set_bind_policy; 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; } mgerdin@5644: static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; } iveresov@897: static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; } iveresov@3024: static int sched_getcpu_syscall(void); 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: } mgerdin@5644: static void numa_set_bind_policy(int policy) { mgerdin@5644: if (_numa_set_bind_policy != NULL) { mgerdin@5644: _numa_set_bind_policy(policy); mgerdin@5644: } mgerdin@5644: } iveresov@576: static int get_node_by_cpu(int cpu_id); duke@435: }; duke@435: duke@435: zgu@3900: 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; dholmes@5679: status = pthread_cond_init (_cond, os::Linux::condAttr()); 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 () ; dholmes@5679: int park (jlong millis) ; // relative timed-wait only duke@435: void SetAssociation (Thread * a) { _Assoc = a ; } duke@435: } ; duke@435: zgu@3900: class PlatformParker : public CHeapObj { duke@435: protected: dholmes@5679: enum { dholmes@5679: REL_INDEX = 0, dholmes@5679: ABS_INDEX = 1 dholmes@5679: }; dholmes@5679: int _cur_index; // which cond is in use: -1, 0, 1 duke@435: pthread_mutex_t _mutex [1] ; dholmes@5679: pthread_cond_t _cond [2] ; // one for relative times and one for abs. 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; dholmes@5679: status = pthread_cond_init (&_cond[REL_INDEX], os::Linux::condAttr()); dholmes@5679: assert_status(status == 0, status, "cond_init rel"); dholmes@5679: status = pthread_cond_init (&_cond[ABS_INDEX], NULL); dholmes@5679: assert_status(status == 0, status, "cond_init abs"); duke@435: status = pthread_mutex_init (_mutex, NULL); duke@435: assert_status(status == 0, status, "mutex_init"); dholmes@5679: _cur_index = -1; // mark as unused duke@435: } sla@5237: }; stefank@2314: stefank@2314: #endif // OS_LINUX_VM_OS_LINUX_HPP