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