aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@1: /* aoqi@1: * This file has been modified by Loongson Technology in 2015. These aoqi@1: * modifications are Copyright (c) 2015 Loongson Technology, and are made aoqi@1: * available on the same license terms set forth above. aoqi@1: */ aoqi@1: aoqi@0: #ifndef SHARE_VM_RUNTIME_OS_HPP aoqi@0: #define SHARE_VM_RUNTIME_OS_HPP aoqi@0: aoqi@0: #include "jvmtifiles/jvmti.h" aoqi@0: #include "runtime/atomic.hpp" aoqi@0: #include "runtime/extendedPC.hpp" aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: #ifdef TARGET_OS_FAMILY_linux aoqi@0: # include "jvm_linux.h" aoqi@0: # include aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_solaris aoqi@0: # include "jvm_solaris.h" aoqi@0: # include aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_windows aoqi@0: # include "jvm_windows.h" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_aix aoqi@0: # include "jvm_aix.h" aoqi@0: # include aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_bsd aoqi@0: # include "jvm_bsd.h" aoqi@0: # include aoqi@0: # ifdef __APPLE__ aoqi@0: # include aoqi@0: # endif aoqi@0: #endif aoqi@0: aoqi@0: class AgentLibrary; aoqi@0: aoqi@0: // os defines the interface to operating system; this includes traditional aoqi@0: // OS services (time, I/O) as well as other functionality with system- aoqi@0: // dependent code. aoqi@0: aoqi@0: typedef void (*dll_func)(...); aoqi@0: aoqi@0: class Thread; aoqi@0: class JavaThread; aoqi@0: class Event; aoqi@0: class DLL; aoqi@0: class FileHandle; aoqi@0: template class GrowableArray; aoqi@0: aoqi@0: // %%%%% Moved ThreadState, START_FN, OSThread to new osThread.hpp. -- Rose aoqi@0: aoqi@0: // Platform-independent error return values from OS functions aoqi@0: enum OSReturn { aoqi@0: OS_OK = 0, // Operation was successful aoqi@0: OS_ERR = -1, // Operation failed aoqi@0: OS_INTRPT = -2, // Operation was interrupted aoqi@0: OS_TIMEOUT = -3, // Operation timed out aoqi@0: OS_NOMEM = -5, // Operation failed for lack of memory aoqi@0: OS_NORESOURCE = -6 // Operation failed for lack of nonmemory resource aoqi@0: }; aoqi@0: aoqi@0: enum ThreadPriority { // JLS 20.20.1-3 aoqi@0: NoPriority = -1, // Initial non-priority value aoqi@0: MinPriority = 1, // Minimum priority aoqi@0: NormPriority = 5, // Normal (non-daemon) priority aoqi@0: NearMaxPriority = 9, // High priority, used for VMThread aoqi@0: MaxPriority = 10, // Highest priority, used for WatcherThread aoqi@0: // ensures that VMThread doesn't starve profiler aoqi@0: CriticalPriority = 11 // Critical thread priority aoqi@0: }; aoqi@0: aoqi@0: // Executable parameter flag for os::commit_memory() and aoqi@0: // os::commit_memory_or_exit(). aoqi@0: const bool ExecMem = true; aoqi@0: aoqi@0: // Typedef for structured exception handling support aoqi@0: typedef void (*java_call_t)(JavaValue* value, methodHandle* method, JavaCallArguments* args, Thread* thread); aoqi@0: aoqi@0: class os: AllStatic { aoqi@0: friend class VMStructs; aoqi@0: aoqi@0: public: aoqi@0: enum { page_sizes_max = 9 }; // Size of _page_sizes array (8 plus a sentinel) aoqi@0: aoqi@0: private: aoqi@0: static OSThread* _starting_thread; aoqi@0: static address _polling_page; aoqi@0: static volatile int32_t * _mem_serialize_page; aoqi@0: static uintptr_t _serialize_page_mask; aoqi@0: public: aoqi@0: static size_t _page_sizes[page_sizes_max]; aoqi@0: aoqi@0: private: aoqi@0: static void init_page_sizes(size_t default_page_size) { aoqi@0: _page_sizes[0] = default_page_size; aoqi@0: _page_sizes[1] = 0; // sentinel aoqi@0: } aoqi@0: aoqi@0: static char* pd_reserve_memory(size_t bytes, char* addr = 0, aoqi@0: size_t alignment_hint = 0); aoqi@0: static char* pd_attempt_reserve_memory_at(size_t bytes, char* addr); aoqi@0: static void pd_split_reserved_memory(char *base, size_t size, aoqi@0: size_t split, bool realloc); aoqi@0: static bool pd_commit_memory(char* addr, size_t bytes, bool executable); aoqi@0: static bool pd_commit_memory(char* addr, size_t size, size_t alignment_hint, aoqi@0: bool executable); aoqi@0: // Same as pd_commit_memory() that either succeeds or calls aoqi@0: // vm_exit_out_of_memory() with the specified mesg. aoqi@0: static void pd_commit_memory_or_exit(char* addr, size_t bytes, aoqi@0: bool executable, const char* mesg); aoqi@0: static void pd_commit_memory_or_exit(char* addr, size_t size, aoqi@0: size_t alignment_hint, aoqi@0: bool executable, const char* mesg); aoqi@0: static bool pd_uncommit_memory(char* addr, size_t bytes); aoqi@0: static bool pd_release_memory(char* addr, size_t bytes); aoqi@0: aoqi@0: static char* pd_map_memory(int fd, const char* file_name, size_t file_offset, aoqi@0: char *addr, size_t bytes, bool read_only = false, aoqi@0: bool allow_exec = false); aoqi@0: static char* pd_remap_memory(int fd, const char* file_name, size_t file_offset, aoqi@0: char *addr, size_t bytes, bool read_only, aoqi@0: bool allow_exec); aoqi@0: static bool pd_unmap_memory(char *addr, size_t bytes); aoqi@0: static void pd_free_memory(char *addr, size_t bytes, size_t alignment_hint); aoqi@0: static void pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint); aoqi@0: aoqi@0: aoqi@0: public: aoqi@0: static void init(void); // Called before command line parsing aoqi@0: static void init_before_ergo(void); // Called after command line parsing aoqi@0: // before VM ergonomics processing. aoqi@0: static jint init_2(void); // Called after command line parsing aoqi@0: // and VM ergonomics processing aoqi@0: static void init_globals(void) { // Called from init_globals() in init.cpp aoqi@0: init_globals_ext(); aoqi@0: } aoqi@0: static void init_3(void); // Called at the end of vm init aoqi@0: aoqi@0: // File names are case-insensitive on windows only aoqi@0: // Override me as needed aoqi@0: static int file_name_strcmp(const char* s1, const char* s2); aoqi@0: aoqi@0: static bool getenv(const char* name, char* buffer, int len); aoqi@0: static bool have_special_privileges(); aoqi@0: aoqi@0: static jlong javaTimeMillis(); aoqi@0: static jlong javaTimeNanos(); aoqi@0: static void javaTimeNanos_info(jvmtiTimerInfo *info_ptr); aoqi@0: static void run_periodic_checks(); aoqi@0: aoqi@0: aoqi@0: // Returns the elapsed time in seconds since the vm started. aoqi@0: static double elapsedTime(); aoqi@0: aoqi@0: // Returns real time in seconds since an arbitrary point aoqi@0: // in the past. aoqi@0: static bool getTimesSecs(double* process_real_time, aoqi@0: double* process_user_time, aoqi@0: double* process_system_time); aoqi@0: aoqi@0: // Interface to the performance counter aoqi@0: static jlong elapsed_counter(); aoqi@0: static jlong elapsed_frequency(); aoqi@0: aoqi@0: // The "virtual time" of a thread is the amount of time a thread has aoqi@0: // actually run. The first function indicates whether the OS supports aoqi@0: // this functionality for the current thread, and if so: aoqi@0: // * the second enables vtime tracking (if that is required). aoqi@0: // * the third tells whether vtime is enabled. aoqi@0: // * the fourth returns the elapsed virtual time for the current aoqi@0: // thread. aoqi@0: static bool supports_vtime(); aoqi@0: static bool enable_vtime(); aoqi@0: static bool vtime_enabled(); aoqi@0: static double elapsedVTime(); aoqi@0: aoqi@0: // Return current local time in a string (YYYY-MM-DD HH:MM:SS). aoqi@0: // It is MT safe, but not async-safe, as reading time zone aoqi@0: // information may require a lock on some platforms. aoqi@0: static char* local_time_string(char *buf, size_t buflen); aoqi@0: static struct tm* localtime_pd (const time_t* clock, struct tm* res); aoqi@0: // Fill in buffer with current local time as an ISO-8601 string. aoqi@0: // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz. aoqi@0: // Returns buffer, or NULL if it failed. aoqi@0: static char* iso8601_time(char* buffer, size_t buffer_length); aoqi@0: aoqi@0: // Interface for detecting multiprocessor system aoqi@0: static inline bool is_MP() { aoqi@0: assert(_processor_count > 0, "invalid processor count"); aoqi@0: return _processor_count > 1 || AssumeMP; aoqi@0: } aoqi@0: static julong available_memory(); aoqi@0: static julong physical_memory(); aoqi@0: static bool has_allocatable_memory_limit(julong* limit); aoqi@0: static bool is_server_class_machine(); aoqi@0: aoqi@0: // number of CPUs aoqi@0: static int processor_count() { aoqi@0: return _processor_count; aoqi@0: } aoqi@0: static void set_processor_count(int count) { _processor_count = count; } aoqi@0: aoqi@0: // Returns the number of CPUs this process is currently allowed to run on. aoqi@0: // Note that on some OSes this can change dynamically. aoqi@0: static int active_processor_count(); aoqi@0: aoqi@0: // Bind processes to processors. aoqi@0: // This is a two step procedure: aoqi@0: // first you generate a distribution of processes to processors, aoqi@0: // then you bind processes according to that distribution. aoqi@0: // Compute a distribution for number of processes to processors. aoqi@0: // Stores the processor id's into the distribution array argument. aoqi@0: // Returns true if it worked, false if it didn't. aoqi@0: static bool distribute_processes(uint length, uint* distribution); aoqi@0: // Binds the current process to a processor. aoqi@0: // Returns true if it worked, false if it didn't. aoqi@0: static bool bind_to_processor(uint processor_id); aoqi@0: aoqi@0: // Give a name to the current thread. aoqi@0: static void set_native_thread_name(const char *name); aoqi@0: aoqi@0: // Interface for stack banging (predetect possible stack overflow for aoqi@0: // exception processing) There are guard pages, and above that shadow aoqi@0: // pages for stack overflow checking. aoqi@0: static bool uses_stack_guard_pages(); aoqi@0: static bool allocate_stack_guard_pages(); aoqi@0: static void bang_stack_shadow_pages(); aoqi@0: static bool stack_shadow_pages_available(Thread *thread, methodHandle method); aoqi@0: aoqi@0: // OS interface to Virtual Memory aoqi@0: aoqi@0: // Return the default page size. aoqi@0: static int vm_page_size(); aoqi@0: aoqi@0: // Return the page size to use for a region of memory. The min_pages argument aoqi@0: // is a hint intended to limit fragmentation; it says the returned page size aoqi@0: // should be <= region_max_size / min_pages. Because min_pages is a hint, aoqi@0: // this routine may return a size larger than region_max_size / min_pages. aoqi@0: // aoqi@0: // The current implementation ignores min_pages if a larger page size is an aoqi@0: // exact multiple of both region_min_size and region_max_size. This allows aoqi@0: // larger pages to be used when doing so would not cause fragmentation; in aoqi@0: // particular, a single page can be used when region_min_size == aoqi@0: // region_max_size == a supported page size. aoqi@0: static size_t page_size_for_region(size_t region_min_size, aoqi@0: size_t region_max_size, aoqi@0: uint min_pages); aoqi@0: // Return the largest page size that can be used aoqi@0: static size_t max_page_size() { aoqi@0: // The _page_sizes array is sorted in descending order. aoqi@0: return _page_sizes[0]; aoqi@0: } aoqi@0: aoqi@0: // Methods for tracing page sizes returned by the above method; enabled by aoqi@0: // TracePageSizes. The region_{min,max}_size parameters should be the values aoqi@0: // passed to page_size_for_region() and page_size should be the result of that aoqi@0: // call. The (optional) base and size parameters should come from the aoqi@0: // ReservedSpace base() and size() methods. aoqi@0: static void trace_page_sizes(const char* str, const size_t* page_sizes, aoqi@0: int count) PRODUCT_RETURN; aoqi@0: static void trace_page_sizes(const char* str, const size_t region_min_size, aoqi@0: const size_t region_max_size, aoqi@0: const size_t page_size, aoqi@0: const char* base = NULL, aoqi@0: const size_t size = 0) PRODUCT_RETURN; aoqi@0: aoqi@0: static int vm_allocation_granularity(); aoqi@0: static char* reserve_memory(size_t bytes, char* addr = 0, aoqi@0: size_t alignment_hint = 0); aoqi@0: static char* reserve_memory(size_t bytes, char* addr, aoqi@0: size_t alignment_hint, MEMFLAGS flags); aoqi@0: static char* reserve_memory_aligned(size_t size, size_t alignment); aoqi@0: static char* attempt_reserve_memory_at(size_t bytes, char* addr); aoqi@0: static void split_reserved_memory(char *base, size_t size, aoqi@0: size_t split, bool realloc); aoqi@0: static bool commit_memory(char* addr, size_t bytes, bool executable); aoqi@0: static bool commit_memory(char* addr, size_t size, size_t alignment_hint, aoqi@0: bool executable); aoqi@0: // Same as commit_memory() that either succeeds or calls aoqi@0: // vm_exit_out_of_memory() with the specified mesg. aoqi@0: static void commit_memory_or_exit(char* addr, size_t bytes, aoqi@0: bool executable, const char* mesg); aoqi@0: static void commit_memory_or_exit(char* addr, size_t size, aoqi@0: size_t alignment_hint, aoqi@0: bool executable, const char* mesg); aoqi@0: static bool uncommit_memory(char* addr, size_t bytes); aoqi@0: static bool release_memory(char* addr, size_t bytes); aoqi@0: aoqi@0: enum ProtType { MEM_PROT_NONE, MEM_PROT_READ, MEM_PROT_RW, MEM_PROT_RWX }; aoqi@0: static bool protect_memory(char* addr, size_t bytes, ProtType prot, aoqi@0: bool is_committed = true); aoqi@0: aoqi@0: static bool guard_memory(char* addr, size_t bytes); aoqi@0: static bool unguard_memory(char* addr, size_t bytes); aoqi@0: static bool create_stack_guard_pages(char* addr, size_t bytes); aoqi@0: static bool pd_create_stack_guard_pages(char* addr, size_t bytes); aoqi@0: static bool remove_stack_guard_pages(char* addr, size_t bytes); aoqi@0: aoqi@0: static char* map_memory(int fd, const char* file_name, size_t file_offset, aoqi@0: char *addr, size_t bytes, bool read_only = false, aoqi@0: bool allow_exec = false); aoqi@0: static char* remap_memory(int fd, const char* file_name, size_t file_offset, aoqi@0: char *addr, size_t bytes, bool read_only, aoqi@0: bool allow_exec); aoqi@0: static bool unmap_memory(char *addr, size_t bytes); aoqi@0: static void free_memory(char *addr, size_t bytes, size_t alignment_hint); aoqi@0: static void realign_memory(char *addr, size_t bytes, size_t alignment_hint); aoqi@0: aoqi@0: // NUMA-specific interface aoqi@0: static bool numa_has_static_binding(); aoqi@0: static bool numa_has_group_homing(); aoqi@0: static void numa_make_local(char *addr, size_t bytes, int lgrp_hint); aoqi@0: static void numa_make_global(char *addr, size_t bytes); aoqi@0: static size_t numa_get_groups_num(); aoqi@0: static size_t numa_get_leaf_groups(int *ids, size_t size); aoqi@0: static bool numa_topology_changed(); aoqi@0: static int numa_get_group_id(); aoqi@1: static int numa_get_cpu_id(); aoqi@0: aoqi@0: // Page manipulation aoqi@0: struct page_info { aoqi@0: size_t size; aoqi@0: int lgrp_id; aoqi@0: }; aoqi@0: static bool get_page_info(char *start, page_info* info); aoqi@0: static char* scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found); aoqi@0: aoqi@0: static char* non_memory_address_word(); aoqi@0: // reserve, commit and pin the entire memory region aoqi@0: static char* reserve_memory_special(size_t size, size_t alignment, aoqi@0: char* addr, bool executable); aoqi@0: static bool release_memory_special(char* addr, size_t bytes); aoqi@0: static void large_page_init(); aoqi@0: static size_t large_page_size(); aoqi@0: static bool can_commit_large_page_memory(); aoqi@0: static bool can_execute_large_page_memory(); aoqi@0: aoqi@0: // OS interface to polling page aoqi@0: static address get_polling_page() { return _polling_page; } aoqi@0: static void set_polling_page(address page) { _polling_page = page; } aoqi@0: static bool is_poll_address(address addr) { return addr >= _polling_page && addr < (_polling_page + os::vm_page_size()); } aoqi@0: static void make_polling_page_unreadable(); aoqi@0: static void make_polling_page_readable(); aoqi@0: aoqi@0: // Routines used to serialize the thread state without using membars aoqi@0: static void serialize_thread_states(); aoqi@0: aoqi@0: // Since we write to the serialize page from every thread, we aoqi@0: // want stores to be on unique cache lines whenever possible aoqi@0: // in order to minimize CPU cross talk. We pre-compute the aoqi@0: // amount to shift the thread* to make this offset unique to aoqi@0: // each thread. aoqi@0: static int get_serialize_page_shift_count() { aoqi@0: return SerializePageShiftCount; aoqi@0: } aoqi@0: aoqi@0: static void set_serialize_page_mask(uintptr_t mask) { aoqi@0: _serialize_page_mask = mask; aoqi@0: } aoqi@0: aoqi@0: static unsigned int get_serialize_page_mask() { aoqi@0: return _serialize_page_mask; aoqi@0: } aoqi@0: aoqi@0: static void set_memory_serialize_page(address page); aoqi@0: aoqi@0: static address get_memory_serialize_page() { aoqi@0: return (address)_mem_serialize_page; aoqi@0: } aoqi@0: aoqi@0: static inline void write_memory_serialize_page(JavaThread *thread) { aoqi@0: uintptr_t page_offset = ((uintptr_t)thread >> aoqi@0: get_serialize_page_shift_count()) & aoqi@0: get_serialize_page_mask(); aoqi@0: *(volatile int32_t *)((uintptr_t)_mem_serialize_page+page_offset) = 1; aoqi@0: } aoqi@0: aoqi@0: static bool is_memory_serialize_page(JavaThread *thread, address addr) { aoqi@0: if (UseMembar) return false; aoqi@0: // Previously this function calculated the exact address of this aoqi@0: // thread's serialize page, and checked if the faulting address aoqi@0: // was equal. However, some platforms mask off faulting addresses aoqi@0: // to the page size, so now we just check that the address is aoqi@0: // within the page. This makes the thread argument unnecessary, aoqi@0: // but we retain the NULL check to preserve existing behaviour. aoqi@0: if (thread == NULL) return false; aoqi@0: address page = (address) _mem_serialize_page; aoqi@0: return addr >= page && addr < (page + os::vm_page_size()); aoqi@0: } aoqi@0: aoqi@0: static void block_on_serialize_page_trap(); aoqi@0: aoqi@0: // threads aoqi@0: aoqi@0: enum ThreadType { aoqi@0: vm_thread, aoqi@0: cgc_thread, // Concurrent GC thread aoqi@0: pgc_thread, // Parallel GC thread aoqi@0: java_thread, aoqi@0: compiler_thread, aoqi@0: watcher_thread, aoqi@0: os_thread aoqi@0: }; aoqi@0: aoqi@0: static bool create_thread(Thread* thread, aoqi@0: ThreadType thr_type, aoqi@0: size_t stack_size = 0); aoqi@0: static bool create_main_thread(JavaThread* thread); aoqi@0: static bool create_attached_thread(JavaThread* thread); aoqi@0: static void pd_start_thread(Thread* thread); aoqi@0: static void start_thread(Thread* thread); aoqi@0: aoqi@0: static void initialize_thread(Thread* thr); aoqi@0: static void free_thread(OSThread* osthread); aoqi@0: aoqi@0: // thread id on Linux/64bit is 64bit, on Windows and Solaris, it's 32bit aoqi@0: static intx current_thread_id(); aoqi@0: static int current_process_id(); aoqi@0: static int sleep(Thread* thread, jlong ms, bool interruptable); aoqi@0: // Short standalone OS sleep suitable for slow path spin loop. aoqi@0: // Ignores Thread.interrupt() (so keep it short). aoqi@0: // ms = 0, will sleep for the least amount of time allowed by the OS. aoqi@0: static void naked_short_sleep(jlong ms); aoqi@0: static void infinite_sleep(); // never returns, use with CAUTION aoqi@0: static void yield(); // Yields to all threads with same priority aoqi@0: enum YieldResult { aoqi@0: YIELD_SWITCHED = 1, // caller descheduled, other ready threads exist & ran aoqi@0: YIELD_NONEREADY = 0, // No other runnable/ready threads. aoqi@0: // platform-specific yield return immediately aoqi@0: YIELD_UNKNOWN = -1 // Unknown: platform doesn't support _SWITCHED or _NONEREADY aoqi@0: // YIELD_SWITCHED and YIELD_NONREADY imply the platform supports a "strong" aoqi@0: // yield that can be used in lieu of blocking. aoqi@0: } ; aoqi@0: static YieldResult NakedYield () ; aoqi@0: static void yield_all(int attempts = 0); // Yields to all other threads including lower priority aoqi@0: static void loop_breaker(int attempts); // called from within tight loops to possibly influence time-sharing aoqi@0: static OSReturn set_priority(Thread* thread, ThreadPriority priority); aoqi@0: static OSReturn get_priority(const Thread* const thread, ThreadPriority& priority); aoqi@0: aoqi@0: static void interrupt(Thread* thread); aoqi@0: static bool is_interrupted(Thread* thread, bool clear_interrupted); aoqi@0: aoqi@0: static int pd_self_suspend_thread(Thread* thread); aoqi@0: aoqi@0: static ExtendedPC fetch_frame_from_context(void* ucVoid, intptr_t** sp, intptr_t** fp); aoqi@0: static frame fetch_frame_from_context(void* ucVoid); aoqi@0: aoqi@0: static ExtendedPC get_thread_pc(Thread *thread); aoqi@0: static void breakpoint(); aoqi@0: aoqi@0: static address current_stack_pointer(); aoqi@0: static address current_stack_base(); aoqi@0: static size_t current_stack_size(); aoqi@0: aoqi@0: static void verify_stack_alignment() PRODUCT_RETURN; aoqi@0: aoqi@0: static int message_box(const char* title, const char* message); aoqi@0: static char* do_you_want_to_debug(const char* message); aoqi@0: aoqi@0: // run cmd in a separate process and return its exit code; or -1 on failures aoqi@0: static int fork_and_exec(char *cmd); aoqi@0: aoqi@0: // os::exit() is merged with vm_exit() aoqi@0: // static void exit(int num); aoqi@0: aoqi@0: // Terminate the VM, but don't exit the process aoqi@0: static void shutdown(); aoqi@0: aoqi@0: // Terminate with an error. Default is to generate a core file on platforms aoqi@0: // that support such things. This calls shutdown() and then aborts. aoqi@0: static void abort(bool dump_core = true); aoqi@0: aoqi@0: // Die immediately, no exit hook, no abort hook, no cleanup. aoqi@0: static void die(); aoqi@0: aoqi@0: // File i/o operations aoqi@0: static const int default_file_open_flags(); aoqi@0: static int open(const char *path, int oflag, int mode); aoqi@0: static FILE* open(int fd, const char* mode); aoqi@0: static int close(int fd); aoqi@0: static jlong lseek(int fd, jlong offset, int whence); aoqi@0: static char* native_path(char *path); aoqi@0: static int ftruncate(int fd, jlong length); aoqi@0: static int fsync(int fd); aoqi@0: static int available(int fd, jlong *bytes); aoqi@0: aoqi@0: //File i/o operations aoqi@0: aoqi@0: static size_t read(int fd, void *buf, unsigned int nBytes); aoqi@0: static size_t restartable_read(int fd, void *buf, unsigned int nBytes); aoqi@0: static size_t write(int fd, const void *buf, unsigned int nBytes); aoqi@0: aoqi@0: // Reading directories. aoqi@0: static DIR* opendir(const char* dirname); aoqi@0: static int readdir_buf_size(const char *path); aoqi@0: static struct dirent* readdir(DIR* dirp, dirent* dbuf); aoqi@0: static int closedir(DIR* dirp); aoqi@0: aoqi@0: // Dynamic library extension aoqi@0: static const char* dll_file_extension(); aoqi@0: aoqi@0: static const char* get_temp_directory(); aoqi@0: static const char* get_current_directory(char *buf, size_t buflen); aoqi@0: aoqi@0: // Builds a platform-specific full library path given a ld path and lib name aoqi@0: // Returns true if buffer contains full path to existing file, false otherwise aoqi@0: static bool dll_build_name(char* buffer, size_t size, aoqi@0: const char* pathname, const char* fname); aoqi@0: aoqi@0: // Symbol lookup, find nearest function name; basically it implements aoqi@0: // dladdr() for all platforms. Name of the nearest function is copied aoqi@0: // to buf. Distance from its base address is optionally returned as offset. aoqi@0: // If function name is not found, buf[0] is set to '\0' and offset is aoqi@0: // set to -1 (if offset is non-NULL). aoqi@0: static bool dll_address_to_function_name(address addr, char* buf, aoqi@0: int buflen, int* offset); aoqi@0: aoqi@0: // Locate DLL/DSO. On success, full path of the library is copied to aoqi@0: // buf, and offset is optionally set to be the distance between addr aoqi@0: // and the library's base address. On failure, buf[0] is set to '\0' aoqi@0: // and offset is set to -1 (if offset is non-NULL). aoqi@0: static bool dll_address_to_library_name(address addr, char* buf, aoqi@0: int buflen, int* offset); aoqi@0: aoqi@0: // Find out whether the pc is in the static code for jvm.dll/libjvm.so. aoqi@0: static bool address_is_in_vm(address addr); aoqi@0: aoqi@0: // Loads .dll/.so and aoqi@0: // in case of error it checks if .dll/.so was built for the aoqi@0: // same architecture as Hotspot is running on aoqi@0: static void* dll_load(const char *name, char *ebuf, int ebuflen); aoqi@0: aoqi@0: // lookup symbol in a shared library aoqi@0: static void* dll_lookup(void* handle, const char* name); aoqi@0: aoqi@0: // Unload library aoqi@0: static void dll_unload(void *lib); aoqi@0: aoqi@0: // Return the handle of this process aoqi@0: static void* get_default_process_handle(); aoqi@0: aoqi@0: // Check for static linked agent library aoqi@0: static bool find_builtin_agent(AgentLibrary *agent_lib, const char *syms[], aoqi@0: size_t syms_len); aoqi@0: aoqi@0: // Find agent entry point aoqi@0: static void *find_agent_function(AgentLibrary *agent_lib, bool check_lib, aoqi@0: const char *syms[], size_t syms_len); aoqi@0: aoqi@0: // Print out system information; they are called by fatal error handler. aoqi@0: // Output format may be different on different platforms. aoqi@0: static void print_os_info(outputStream* st); aoqi@0: static void print_os_info_brief(outputStream* st); aoqi@0: static void print_cpu_info(outputStream* st); aoqi@0: static void pd_print_cpu_info(outputStream* st); aoqi@0: static void print_memory_info(outputStream* st); aoqi@0: static void print_dll_info(outputStream* st); aoqi@0: static void print_environment_variables(outputStream* st, const char** env_list, char* buffer, int len); aoqi@0: static void print_context(outputStream* st, void* context); aoqi@0: static void print_register_info(outputStream* st, void* context); aoqi@0: static void print_siginfo(outputStream* st, void* siginfo); aoqi@0: static void print_signal_handlers(outputStream* st, char* buf, size_t buflen); aoqi@0: static void print_date_and_time(outputStream* st); aoqi@0: aoqi@0: static void print_location(outputStream* st, intptr_t x, bool verbose = false); aoqi@0: static size_t lasterror(char *buf, size_t len); aoqi@0: static int get_last_error(); aoqi@0: aoqi@0: // Determines whether the calling process is being debugged by a user-mode debugger. aoqi@0: static bool is_debugger_attached(); aoqi@0: aoqi@0: // wait for a key press if PauseAtExit is set aoqi@0: static void wait_for_keypress_at_exit(void); aoqi@0: aoqi@0: // The following two functions are used by fatal error handler to trace aoqi@0: // native (C) frames. They are not part of frame.hpp/frame.cpp because aoqi@0: // frame.hpp/cpp assume thread is JavaThread, and also because different aoqi@0: // OS/compiler may have different convention or provide different API to aoqi@0: // walk C frames. aoqi@0: // aoqi@0: // We don't attempt to become a debugger, so we only follow frames if that aoqi@0: // does not require a lookup in the unwind table, which is part of the binary aoqi@0: // file but may be unsafe to read after a fatal error. So on x86, we can aoqi@0: // only walk stack if %ebp is used as frame pointer; on ia64, it's not aoqi@0: // possible to walk C stack without having the unwind table. aoqi@0: static bool is_first_C_frame(frame *fr); aoqi@0: static frame get_sender_for_C_frame(frame *fr); aoqi@0: aoqi@0: // return current frame. pc() and sp() are set to NULL on failure. aoqi@0: static frame current_frame(); aoqi@0: aoqi@0: static void print_hex_dump(outputStream* st, address start, address end, int unitsize); aoqi@0: aoqi@0: // returns a string to describe the exception/signal; aoqi@0: // returns NULL if exception_code is not an OS exception/signal. aoqi@0: static const char* exception_name(int exception_code, char* buf, size_t buflen); aoqi@0: aoqi@0: // Returns native Java library, loads if necessary aoqi@0: static void* native_java_library(); aoqi@0: aoqi@0: // Fills in path to jvm.dll/libjvm.so (used by the Disassembler) aoqi@0: static void jvm_path(char *buf, jint buflen); aoqi@0: aoqi@0: // Returns true if we are running in a headless jre. aoqi@0: static bool is_headless_jre(); aoqi@0: aoqi@0: // JNI names aoqi@0: static void print_jni_name_prefix_on(outputStream* st, int args_size); aoqi@0: static void print_jni_name_suffix_on(outputStream* st, int args_size); aoqi@0: aoqi@0: // File conventions aoqi@0: static const char* file_separator(); aoqi@0: static const char* line_separator(); aoqi@0: static const char* path_separator(); aoqi@0: aoqi@0: // Init os specific system properties values aoqi@0: static void init_system_properties_values(); aoqi@0: aoqi@0: // IO operations, non-JVM_ version. aoqi@0: static int stat(const char* path, struct stat* sbuf); aoqi@0: static bool dir_is_empty(const char* path); aoqi@0: aoqi@0: // IO operations on binary files aoqi@0: static int create_binary_file(const char* path, bool rewrite_existing); aoqi@0: static jlong current_file_offset(int fd); aoqi@0: static jlong seek_to_file_offset(int fd, jlong offset); aoqi@0: aoqi@0: // Thread Local Storage aoqi@0: static int allocate_thread_local_storage(); aoqi@0: static void thread_local_storage_at_put(int index, void* value); aoqi@0: static void* thread_local_storage_at(int index); aoqi@0: static void free_thread_local_storage(int index); aoqi@0: aoqi@0: // Stack walk aoqi@0: static address get_caller_pc(int n = 0); aoqi@0: aoqi@0: // General allocation (must be MT-safe) aoqi@0: static void* malloc (size_t size, MEMFLAGS flags, address caller_pc = 0); aoqi@0: static void* realloc (void *memblock, size_t size, MEMFLAGS flags, address caller_pc = 0); aoqi@0: static void free (void *memblock, MEMFLAGS flags = mtNone); aoqi@0: static bool check_heap(bool force = false); // verify C heap integrity aoqi@0: static char* strdup(const char *, MEMFLAGS flags = mtInternal); // Like strdup aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: static julong num_mallocs; // # of calls to malloc/realloc aoqi@0: static julong alloc_bytes; // # of bytes allocated aoqi@0: static julong num_frees; // # of calls to free aoqi@0: static julong free_bytes; // # of bytes freed aoqi@0: #endif aoqi@0: aoqi@0: // SocketInterface (ex HPI SocketInterface ) aoqi@0: static int socket(int domain, int type, int protocol); aoqi@0: static int socket_close(int fd); aoqi@0: static int socket_shutdown(int fd, int howto); aoqi@0: static int recv(int fd, char* buf, size_t nBytes, uint flags); aoqi@0: static int send(int fd, char* buf, size_t nBytes, uint flags); aoqi@0: static int raw_send(int fd, char* buf, size_t nBytes, uint flags); aoqi@0: static int timeout(int fd, long timeout); aoqi@0: static int listen(int fd, int count); aoqi@0: static int connect(int fd, struct sockaddr* him, socklen_t len); aoqi@0: static int bind(int fd, struct sockaddr* him, socklen_t len); aoqi@0: static int accept(int fd, struct sockaddr* him, socklen_t* len); aoqi@0: static int recvfrom(int fd, char* buf, size_t nbytes, uint flags, aoqi@0: struct sockaddr* from, socklen_t* fromlen); aoqi@0: static int get_sock_name(int fd, struct sockaddr* him, socklen_t* len); aoqi@0: static int sendto(int fd, char* buf, size_t len, uint flags, aoqi@0: struct sockaddr* to, socklen_t tolen); aoqi@0: static int socket_available(int fd, jint* pbytes); aoqi@0: aoqi@0: static int get_sock_opt(int fd, int level, int optname, aoqi@0: char* optval, socklen_t* optlen); aoqi@0: static int set_sock_opt(int fd, int level, int optname, aoqi@0: const char* optval, socklen_t optlen); aoqi@0: static int get_host_name(char* name, int namelen); aoqi@0: aoqi@0: static struct hostent* get_host_by_name(char* name); aoqi@0: aoqi@0: // Support for signals (see JVM_RaiseSignal, JVM_RegisterSignal) aoqi@0: static void signal_init(); aoqi@0: static void signal_init_pd(); aoqi@0: static void signal_notify(int signal_number); aoqi@0: static void* signal(int signal_number, void* handler); aoqi@0: static void signal_raise(int signal_number); aoqi@0: static int signal_wait(); aoqi@0: static int signal_lookup(); aoqi@0: static void* user_handler(); aoqi@0: static void terminate_signal_thread(); aoqi@0: static int sigexitnum_pd(); aoqi@0: aoqi@0: // random number generation aoqi@0: static long random(); // return 32bit pseudorandom number aoqi@0: static void init_random(long initval); // initialize random sequence aoqi@0: aoqi@0: // Structured OS Exception support aoqi@0: static void os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method, JavaCallArguments* args, Thread* thread); aoqi@0: aoqi@0: // On Windows this will create an actual minidump, on Linux/Solaris it will simply check core dump limits aoqi@0: static void check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize); aoqi@0: aoqi@0: // Get the default path to the core file aoqi@0: // Returns the length of the string aoqi@0: static int get_core_path(char* buffer, size_t bufferSize); aoqi@0: aoqi@0: // JVMTI & JVM monitoring and management support aoqi@0: // The thread_cpu_time() and current_thread_cpu_time() are only aoqi@0: // supported if is_thread_cpu_time_supported() returns true. aoqi@0: // They are not supported on Solaris T1. aoqi@0: aoqi@0: // Thread CPU Time - return the fast estimate on a platform aoqi@0: // On Solaris - call gethrvtime (fast) - user time only aoqi@0: // On Linux - fast clock_gettime where available - user+sys aoqi@0: // - otherwise: very slow /proc fs - user+sys aoqi@0: // On Windows - GetThreadTimes - user+sys aoqi@0: static jlong current_thread_cpu_time(); aoqi@0: static jlong thread_cpu_time(Thread* t); aoqi@0: aoqi@0: // Thread CPU Time with user_sys_cpu_time parameter. aoqi@0: // aoqi@0: // If user_sys_cpu_time is true, user+sys time is returned. aoqi@0: // Otherwise, only user time is returned aoqi@0: static jlong current_thread_cpu_time(bool user_sys_cpu_time); aoqi@0: static jlong thread_cpu_time(Thread* t, bool user_sys_cpu_time); aoqi@0: aoqi@0: // Return a bunch of info about the timers. aoqi@0: // Note that the returned info for these two functions may be different aoqi@0: // on some platforms aoqi@0: static void current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr); aoqi@0: static void thread_cpu_time_info(jvmtiTimerInfo *info_ptr); aoqi@0: aoqi@0: static bool is_thread_cpu_time_supported(); aoqi@0: aoqi@0: // System loadavg support. Returns -1 if load average cannot be obtained. aoqi@0: static int loadavg(double loadavg[], int nelem); aoqi@0: aoqi@0: // Hook for os specific jvm options that we don't want to abort on seeing aoqi@0: static bool obsolete_option(const JavaVMOption *option); aoqi@0: aoqi@0: // Extensions aoqi@0: #include "runtime/os_ext.hpp" aoqi@0: aoqi@0: public: aoqi@0: class CrashProtectionCallback : public StackObj { aoqi@0: public: aoqi@0: virtual void call() = 0; aoqi@0: }; aoqi@0: aoqi@0: // Platform dependent stuff aoqi@0: #ifdef TARGET_OS_FAMILY_linux aoqi@0: # include "os_linux.hpp" aoqi@0: # include "os_posix.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_solaris aoqi@0: # include "os_solaris.hpp" aoqi@0: # include "os_posix.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_windows aoqi@0: # include "os_windows.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_aix aoqi@0: # include "os_aix.hpp" aoqi@0: # include "os_posix.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_bsd aoqi@0: # include "os_posix.hpp" aoqi@0: # include "os_bsd.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_linux_x86 aoqi@0: # include "os_linux_x86.hpp" aoqi@0: #endif aoqi@1: #ifdef TARGET_OS_ARCH_linux_mips aoqi@1: # include "os_linux_mips.hpp" aoqi@1: #endif aoqi@0: #ifdef TARGET_OS_ARCH_linux_sparc aoqi@0: # include "os_linux_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_linux_zero aoqi@0: # include "os_linux_zero.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_solaris_x86 aoqi@0: # include "os_solaris_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_solaris_sparc aoqi@0: # include "os_solaris_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_windows_x86 aoqi@0: # include "os_windows_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_linux_arm aoqi@0: # include "os_linux_arm.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_linux_ppc aoqi@0: # include "os_linux_ppc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_aix_ppc aoqi@0: # include "os_aix_ppc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_bsd_x86 aoqi@0: # include "os_bsd_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_ARCH_bsd_zero aoqi@0: # include "os_bsd_zero.hpp" aoqi@0: #endif aoqi@0: aoqi@0: public: aoqi@0: #ifndef PLATFORM_PRINT_NATIVE_STACK aoqi@0: // No platform-specific code for printing the native stack. aoqi@0: static bool platform_print_native_stack(outputStream* st, void* context, aoqi@0: char *buf, int buf_size) { aoqi@0: return false; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // debugging support (mostly used by debug.cpp but also fatal error handler) aoqi@0: static bool find(address pc, outputStream* st = tty); // OS specific function to make sense out of an address aoqi@0: aoqi@0: static bool dont_yield(); // when true, JVM_Yield() is nop aoqi@0: static void print_statistics(); aoqi@0: aoqi@0: // Thread priority helpers (implemented in OS-specific part) aoqi@0: static OSReturn set_native_priority(Thread* thread, int native_prio); aoqi@0: static OSReturn get_native_priority(const Thread* const thread, int* priority_ptr); aoqi@0: static int java_to_os_priority[CriticalPriority + 1]; aoqi@0: // Hint to the underlying OS that a task switch would not be good. aoqi@0: // Void return because it's a hint and can fail. aoqi@0: static void hint_no_preempt(); aoqi@0: aoqi@0: // Used at creation if requested by the diagnostic flag PauseAtStartup. aoqi@0: // Causes the VM to wait until an external stimulus has been applied aoqi@0: // (for Unix, that stimulus is a signal, for Windows, an external aoqi@0: // ResumeThread call) aoqi@0: static void pause(); aoqi@0: aoqi@0: // Builds a platform dependent Agent_OnLoad_ function name aoqi@0: // which is used to find statically linked in agents. aoqi@0: static char* build_agent_function_name(const char *sym, const char *cname, aoqi@0: bool is_absolute_path); aoqi@0: aoqi@0: class SuspendedThreadTaskContext { aoqi@0: public: aoqi@0: SuspendedThreadTaskContext(Thread* thread, void *ucontext) : _thread(thread), _ucontext(ucontext) {} aoqi@0: Thread* thread() const { return _thread; } aoqi@0: void* ucontext() const { return _ucontext; } aoqi@0: private: aoqi@0: Thread* _thread; aoqi@0: void* _ucontext; aoqi@0: }; aoqi@0: aoqi@0: class SuspendedThreadTask { aoqi@0: public: aoqi@0: SuspendedThreadTask(Thread* thread) : _thread(thread), _done(false) {} aoqi@0: virtual ~SuspendedThreadTask() {} aoqi@0: void run(); aoqi@0: bool is_done() { return _done; } aoqi@0: virtual void do_task(const SuspendedThreadTaskContext& context) = 0; aoqi@0: protected: aoqi@0: private: aoqi@0: void internal_do_task(); aoqi@0: Thread* _thread; aoqi@0: bool _done; aoqi@0: }; aoqi@0: aoqi@0: #ifndef TARGET_OS_FAMILY_windows aoqi@0: // Suspend/resume support aoqi@0: // Protocol: aoqi@0: // aoqi@0: // a thread starts in SR_RUNNING aoqi@0: // aoqi@0: // SR_RUNNING can go to aoqi@0: // * SR_SUSPEND_REQUEST when the WatcherThread wants to suspend it aoqi@0: // SR_SUSPEND_REQUEST can go to aoqi@0: // * SR_RUNNING if WatcherThread decides it waited for SR_SUSPENDED too long (timeout) aoqi@0: // * SR_SUSPENDED if the stopped thread receives the signal and switches state aoqi@0: // SR_SUSPENDED can go to aoqi@0: // * SR_WAKEUP_REQUEST when the WatcherThread has done the work and wants to resume aoqi@0: // SR_WAKEUP_REQUEST can go to aoqi@0: // * SR_RUNNING when the stopped thread receives the signal aoqi@0: // * SR_WAKEUP_REQUEST on timeout (resend the signal and try again) aoqi@0: class SuspendResume { aoqi@0: public: aoqi@0: enum State { aoqi@0: SR_RUNNING, aoqi@0: SR_SUSPEND_REQUEST, aoqi@0: SR_SUSPENDED, aoqi@0: SR_WAKEUP_REQUEST aoqi@0: }; aoqi@0: aoqi@0: private: aoqi@0: volatile State _state; aoqi@0: aoqi@0: private: aoqi@0: /* try to switch state from state "from" to state "to" aoqi@0: * returns the state set after the method is complete aoqi@0: */ aoqi@0: State switch_state(State from, State to); aoqi@0: aoqi@0: public: aoqi@0: SuspendResume() : _state(SR_RUNNING) { } aoqi@0: aoqi@0: State state() const { return _state; } aoqi@0: aoqi@0: State request_suspend() { aoqi@0: return switch_state(SR_RUNNING, SR_SUSPEND_REQUEST); aoqi@0: } aoqi@0: aoqi@0: State cancel_suspend() { aoqi@0: return switch_state(SR_SUSPEND_REQUEST, SR_RUNNING); aoqi@0: } aoqi@0: aoqi@0: State suspended() { aoqi@0: return switch_state(SR_SUSPEND_REQUEST, SR_SUSPENDED); aoqi@0: } aoqi@0: aoqi@0: State request_wakeup() { aoqi@0: return switch_state(SR_SUSPENDED, SR_WAKEUP_REQUEST); aoqi@0: } aoqi@0: aoqi@0: State running() { aoqi@0: return switch_state(SR_WAKEUP_REQUEST, SR_RUNNING); aoqi@0: } aoqi@0: aoqi@0: bool is_running() const { aoqi@0: return _state == SR_RUNNING; aoqi@0: } aoqi@0: aoqi@0: bool is_suspend_request() const { aoqi@0: return _state == SR_SUSPEND_REQUEST; aoqi@0: } aoqi@0: aoqi@0: bool is_suspended() const { aoqi@0: return _state == SR_SUSPENDED; aoqi@0: } aoqi@0: }; aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: protected: aoqi@0: static long _rand_seed; // seed for random number generator aoqi@0: static int _processor_count; // number of processors aoqi@0: aoqi@0: static char* format_boot_path(const char* format_string, aoqi@0: const char* home, aoqi@0: int home_len, aoqi@0: char fileSep, aoqi@0: char pathSep); aoqi@0: static bool set_boot_path(char fileSep, char pathSep); aoqi@0: static char** split_path(const char* path, int* n); aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: // Note that "PAUSE" is almost always used with synchronization aoqi@0: // so arguably we should provide Atomic::SpinPause() instead aoqi@0: // of the global SpinPause() with C linkage. aoqi@0: // It'd also be eligible for inlining on many platforms. aoqi@0: aoqi@0: extern "C" int SpinPause(); aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_OS_HPP