os.hpp revision 7049:396253716f03
18876Srgrimes/* 24Srgrimes * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 34Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 44Srgrimes * 58876Srgrimes * This code is free software; you can redistribute it and/or modify it 64Srgrimes * under the terms of the GNU General Public License version 2 only, as 74Srgrimes * published by the Free Software Foundation. 84Srgrimes * 94Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 104Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 118876Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 128876Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 134Srgrimes * accompanied this code). 144Srgrimes * 158876Srgrimes * You should have received a copy of the GNU General Public License version 164Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 178876Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 184Srgrimes * 194Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 204Srgrimes * or visit www.oracle.com if you need additional information or have any 214Srgrimes * questions. 228876Srgrimes * 234Srgrimes */ 244Srgrimes 254Srgrimes#ifndef SHARE_VM_RUNTIME_OS_HPP 2617495Sjoerg#define SHARE_VM_RUNTIME_OS_HPP 274Srgrimes 28623Srgrimes#include "jvmtifiles/jvmti.h" 294Srgrimes#include "runtime/extendedPC.hpp" 304Srgrimes#include "runtime/handles.hpp" 314Srgrimes#include "utilities/top.hpp" 324Srgrimes#ifdef TARGET_OS_FAMILY_linux 334Srgrimes# include "jvm_linux.h" 342056Swollman# include <setjmp.h> 3517495Sjoerg#endif 362056Swollman#ifdef TARGET_OS_FAMILY_solaris 3712734Sbde# include "jvm_solaris.h" 3812734Sbde# include <setjmp.h> 3912734Sbde#endif 402056Swollman#ifdef TARGET_OS_FAMILY_windows 412056Swollman# include "jvm_windows.h" 424Srgrimes#endif 434Srgrimes#ifdef TARGET_OS_FAMILY_aix 444Srgrimes# include "jvm_aix.h" 454Srgrimes# include <setjmp.h> 464Srgrimes#endif 474Srgrimes#ifdef TARGET_OS_FAMILY_bsd 484Srgrimes# include "jvm_bsd.h" 494Srgrimes# include <setjmp.h> 504Srgrimes# ifdef __APPLE__ 514Srgrimes# include <mach/mach_time.h> 5212720Sphk# endif 5312720Sphk#endif 5412720Sphk 5512720Sphkclass AgentLibrary; 564Srgrimes 5717495Sjoerg// os defines the interface to operating system; this includes traditional 5817495Sjoerg// OS services (time, I/O) as well as other functionality with system- 5917495Sjoerg// dependent code. 6017495Sjoerg 6117495Sjoergtypedef void (*dll_func)(...); 6217495Sjoerg 6317495Sjoergclass Thread; 644Srgrimesclass JavaThread; 654Srgrimesclass Event; 664Srgrimesclass DLL; 674Srgrimesclass FileHandle; 684Srgrimesclass NativeCallStack; 6912515Sphk 7012515Sphktemplate<class E> class GrowableArray; 7112515Sphk 7212515Sphk// %%%%% Moved ThreadState, START_FN, OSThread to new osThread.hpp. -- Rose 7312515Sphk 7412473Sbde// Platform-independent error return values from OS functions 754Srgrimesenum OSReturn { 764Srgrimes OS_OK = 0, // Operation was successful 774Srgrimes OS_ERR = -1, // Operation failed 784Srgrimes OS_INTRPT = -2, // Operation was interrupted 794Srgrimes OS_TIMEOUT = -3, // Operation timed out 804Srgrimes OS_NOMEM = -5, // Operation failed for lack of memory 814Srgrimes OS_NORESOURCE = -6 // Operation failed for lack of nonmemory resource 824Srgrimes}; 834Srgrimes 844Srgrimesenum ThreadPriority { // JLS 20.20.1-3 854Srgrimes NoPriority = -1, // Initial non-priority value 864Srgrimes MinPriority = 1, // Minimum priority 874Srgrimes NormPriority = 5, // Normal (non-daemon) priority 884Srgrimes NearMaxPriority = 9, // High priority, used for VMThread 894Srgrimes MaxPriority = 10, // Highest priority, used for WatcherThread 904Srgrimes // ensures that VMThread doesn't starve profiler 914Srgrimes CriticalPriority = 11 // Critical thread priority 924Srgrimes}; 934Srgrimes 944Srgrimes// Executable parameter flag for os::commit_memory() and 954Srgrimes// os::commit_memory_or_exit(). 964Srgrimesconst bool ExecMem = true; 974Srgrimes 984Srgrimes// Typedef for structured exception handling support 994Srgrimestypedef void (*java_call_t)(JavaValue* value, methodHandle* method, JavaCallArguments* args, Thread* thread); 1004Srgrimes 1014Srgrimesclass MallocTracker; 1024Srgrimes 1034Srgrimesclass os: AllStatic { 1044Srgrimes friend class VMStructs; 1054Srgrimes friend class MallocTracker; 1064Srgrimes public: 1074Srgrimes enum { page_sizes_max = 9 }; // Size of _page_sizes array (8 plus a sentinel) 1084Srgrimes 1094Srgrimes private: 1104Srgrimes static OSThread* _starting_thread; 1114Srgrimes static address _polling_page; 1124Srgrimes static volatile int32_t * _mem_serialize_page; 1134Srgrimes static uintptr_t _serialize_page_mask; 1144Srgrimes public: 1154Srgrimes static size_t _page_sizes[page_sizes_max]; 1164Srgrimes 1174Srgrimes private: 1184Srgrimes static void init_page_sizes(size_t default_page_size) { 1194Srgrimes _page_sizes[0] = default_page_size; 1204Srgrimes _page_sizes[1] = 0; // sentinel 1214Srgrimes } 1224Srgrimes 1234Srgrimes static char* pd_reserve_memory(size_t bytes, char* addr = 0, 1244Srgrimes size_t alignment_hint = 0); 1254Srgrimes static char* pd_attempt_reserve_memory_at(size_t bytes, char* addr); 1264Srgrimes static void pd_split_reserved_memory(char *base, size_t size, 1274Srgrimes size_t split, bool realloc); 1284Srgrimes static bool pd_commit_memory(char* addr, size_t bytes, bool executable); 1294Srgrimes static bool pd_commit_memory(char* addr, size_t size, size_t alignment_hint, 1304Srgrimes bool executable); 1314Srgrimes // Same as pd_commit_memory() that either succeeds or calls 1324Srgrimes // vm_exit_out_of_memory() with the specified mesg. 1334Srgrimes static void pd_commit_memory_or_exit(char* addr, size_t bytes, 1344Srgrimes bool executable, const char* mesg); 1354Srgrimes static void pd_commit_memory_or_exit(char* addr, size_t size, 1364Srgrimes size_t alignment_hint, 1374Srgrimes bool executable, const char* mesg); 1384Srgrimes static bool pd_uncommit_memory(char* addr, size_t bytes); 1394Srgrimes static bool pd_release_memory(char* addr, size_t bytes); 1404Srgrimes 1414Srgrimes static char* pd_map_memory(int fd, const char* file_name, size_t file_offset, 1424Srgrimes char *addr, size_t bytes, bool read_only = false, 1434Srgrimes bool allow_exec = false); 1444Srgrimes static char* pd_remap_memory(int fd, const char* file_name, size_t file_offset, 1454Srgrimes char *addr, size_t bytes, bool read_only, 1464Srgrimes bool allow_exec); 1474Srgrimes static bool pd_unmap_memory(char *addr, size_t bytes); 1484Srgrimes static void pd_free_memory(char *addr, size_t bytes, size_t alignment_hint); 1494Srgrimes static void pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint); 1504Srgrimes 1514Srgrimes 1524Srgrimes public: 1534Srgrimes static void init(void); // Called before command line parsing 1544Srgrimes static void init_before_ergo(void); // Called after command line parsing 1554Srgrimes // before VM ergonomics processing. 1564Srgrimes static jint init_2(void); // Called after command line parsing 1574Srgrimes // and VM ergonomics processing 1584Srgrimes static void init_globals(void) { // Called from init_globals() in init.cpp 1594Srgrimes init_globals_ext(); 1604Srgrimes } 1614Srgrimes static void init_3(void); // Called at the end of vm init 1624Srgrimes 1634Srgrimes // File names are case-insensitive on windows only 1644Srgrimes // Override me as needed 1654Srgrimes static int file_name_strcmp(const char* s1, const char* s2); 1664Srgrimes 1674Srgrimes // get/unset environment variable 1684Srgrimes static bool getenv(const char* name, char* buffer, int len); 1694Srgrimes static bool unsetenv(const char* name); 1704Srgrimes 1714Srgrimes static bool have_special_privileges(); 1724Srgrimes 1734Srgrimes static jlong javaTimeMillis(); 1744Srgrimes static jlong javaTimeNanos(); 1754Srgrimes static void javaTimeNanos_info(jvmtiTimerInfo *info_ptr); 1764Srgrimes static void run_periodic_checks(); 1774Srgrimes static bool supports_monotonic_clock(); 1784Srgrimes 1794Srgrimes 1804Srgrimes // Returns the elapsed time in seconds since the vm started. 1814Srgrimes static double elapsedTime(); 18217495Sjoerg 1834Srgrimes // Returns real time in seconds since an arbitrary point 1844Srgrimes // in the past. 1854Srgrimes static bool getTimesSecs(double* process_real_time, 1864Srgrimes double* process_user_time, 1874Srgrimes double* process_system_time); 18817495Sjoerg 18917495Sjoerg // Interface to the performance counter 19017495Sjoerg static jlong elapsed_counter(); 19117495Sjoerg static jlong elapsed_frequency(); 19217495Sjoerg 19317495Sjoerg // The "virtual time" of a thread is the amount of time a thread has 19417495Sjoerg // actually run. The first function indicates whether the OS supports 19517495Sjoerg // this functionality for the current thread, and if so: 19617495Sjoerg // * the second enables vtime tracking (if that is required). 19717495Sjoerg // * the third tells whether vtime is enabled. 19817495Sjoerg // * the fourth returns the elapsed virtual time for the current 19917495Sjoerg // thread. 20017495Sjoerg static bool supports_vtime(); 20117495Sjoerg static bool enable_vtime(); 20217495Sjoerg static bool vtime_enabled(); 20317495Sjoerg static double elapsedVTime(); 20417495Sjoerg 20517495Sjoerg // Return current local time in a string (YYYY-MM-DD HH:MM:SS). 20617495Sjoerg // It is MT safe, but not async-safe, as reading time zone 20717495Sjoerg // information may require a lock on some platforms. 20817495Sjoerg static char* local_time_string(char *buf, size_t buflen); 20917495Sjoerg static struct tm* localtime_pd (const time_t* clock, struct tm* res); 21017495Sjoerg // Fill in buffer with current local time as an ISO-8601 string. 21117495Sjoerg // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz. 21217495Sjoerg // Returns buffer, or NULL if it failed. 21317495Sjoerg static char* iso8601_time(char* buffer, size_t buffer_length); 21417495Sjoerg 21517495Sjoerg // Interface for detecting multiprocessor system 21617495Sjoerg static inline bool is_MP() { 21717495Sjoerg // During bootstrap if _processor_count is not yet initialized 21817495Sjoerg // we claim to be MP as that is safest. If any platform has a 21917495Sjoerg // stub generator that might be triggered in this phase and for 22017495Sjoerg // which being declared MP when in fact not, is a problem - then 22117495Sjoerg // the bootstrap routine for the stub generator needs to check 2224Srgrimes // the processor count directly and leave the bootstrap routine 2234Srgrimes // in place until called after initialization has ocurred. 2244Srgrimes return (_processor_count != 1) || AssumeMP; 2254Srgrimes } 2264Srgrimes static julong available_memory(); 2274Srgrimes static julong physical_memory(); 2284Srgrimes static bool has_allocatable_memory_limit(julong* limit); 2294Srgrimes static bool is_server_class_machine(); 2304Srgrimes 2314Srgrimes // number of CPUs 2324Srgrimes static int processor_count() { 2334Srgrimes return _processor_count; 2344Srgrimes } 2354Srgrimes static void set_processor_count(int count) { _processor_count = count; } 2364Srgrimes 2374Srgrimes // Returns the number of CPUs this process is currently allowed to run on. 2384Srgrimes // Note that on some OSes this can change dynamically. 2394Srgrimes static int active_processor_count(); 2404Srgrimes 2414Srgrimes // Bind processes to processors. 2424Srgrimes // This is a two step procedure: 2434Srgrimes // first you generate a distribution of processes to processors, 2444Srgrimes // then you bind processes according to that distribution. 2454Srgrimes // Compute a distribution for number of processes to processors. 2464Srgrimes // Stores the processor id's into the distribution array argument. 24712473Sbde // Returns true if it worked, false if it didn't. 2482112Swollman static bool distribute_processes(uint length, uint* distribution); 2492112Swollman // Binds the current process to a processor. 2502112Swollman // Returns true if it worked, false if it didn't. 2512112Swollman static bool bind_to_processor(uint processor_id); 2522112Swollman 2534Srgrimes // Give a name to the current thread. 2544Srgrimes static void set_native_thread_name(const char *name); 2554Srgrimes 2564Srgrimes // Interface for stack banging (predetect possible stack overflow for 25717495Sjoerg // exception processing) There are guard pages, and above that shadow 25817495Sjoerg // pages for stack overflow checking. 25917495Sjoerg static bool uses_stack_guard_pages(); 26017495Sjoerg static bool allocate_stack_guard_pages(); 26117495Sjoerg static void bang_stack_shadow_pages(); 26217495Sjoerg static bool stack_shadow_pages_available(Thread *thread, methodHandle method); 26317495Sjoerg 26417495Sjoerg // OS interface to Virtual Memory 26517495Sjoerg 26617495Sjoerg // Return the default page size. 26717495Sjoerg static int vm_page_size(); 26817495Sjoerg 26917495Sjoerg // Return the page size to use for a region of memory. The min_pages argument 27017495Sjoerg // is a hint intended to limit fragmentation; it says the returned page size 2714Srgrimes // should be <= region_max_size / min_pages. Because min_pages is a hint, 2724Srgrimes // this routine may return a size larger than region_max_size / min_pages. 2734Srgrimes // 2744Srgrimes // The current implementation ignores min_pages if a larger page size is an 2754Srgrimes // exact multiple of both region_min_size and region_max_size. This allows 2764Srgrimes // larger pages to be used when doing so would not cause fragmentation; in 2774Srgrimes // particular, a single page can be used when region_min_size == 2784Srgrimes // region_max_size == a supported page size. 2794Srgrimes static size_t page_size_for_region(size_t region_min_size, 2804Srgrimes size_t region_max_size, 28115680Sgpalmer uint min_pages); 28217495Sjoerg // Return the largest page size that can be used 2834Srgrimes static size_t max_page_size() { 28417495Sjoerg // The _page_sizes array is sorted in descending order. 28517495Sjoerg return _page_sizes[0]; 28617495Sjoerg } 28717495Sjoerg 28817495Sjoerg // Methods for tracing page sizes returned by the above method; enabled by 28917495Sjoerg // TracePageSizes. The region_{min,max}_size parameters should be the values 29017495Sjoerg // passed to page_size_for_region() and page_size should be the result of that 29117495Sjoerg // call. The (optional) base and size parameters should come from the 29217495Sjoerg // ReservedSpace base() and size() methods. 29317495Sjoerg static void trace_page_sizes(const char* str, const size_t* page_sizes, 29417495Sjoerg int count) PRODUCT_RETURN; 29517495Sjoerg static void trace_page_sizes(const char* str, const size_t region_min_size, 2964Srgrimes const size_t region_max_size, 2974Srgrimes const size_t page_size, 2984Srgrimes const char* base = NULL, 2994Srgrimes const size_t size = 0) PRODUCT_RETURN; 3004Srgrimes 3014Srgrimes static int vm_allocation_granularity(); 3024Srgrimes static char* reserve_memory(size_t bytes, char* addr = 0, 3034Srgrimes size_t alignment_hint = 0); 3044Srgrimes static char* reserve_memory(size_t bytes, char* addr, 3054Srgrimes size_t alignment_hint, MEMFLAGS flags); 3064Srgrimes static char* reserve_memory_aligned(size_t size, size_t alignment); 3074Srgrimes static char* attempt_reserve_memory_at(size_t bytes, char* addr); 3084Srgrimes static void split_reserved_memory(char *base, size_t size, 3094Srgrimes size_t split, bool realloc); 3104Srgrimes static bool commit_memory(char* addr, size_t bytes, bool executable); 3114Srgrimes static bool commit_memory(char* addr, size_t size, size_t alignment_hint, 3124Srgrimes bool executable); 3134Srgrimes // Same as commit_memory() that either succeeds or calls 3144Srgrimes // vm_exit_out_of_memory() with the specified mesg. 3154Srgrimes static void commit_memory_or_exit(char* addr, size_t bytes, 3164Srgrimes bool executable, const char* mesg); 3174Srgrimes static void commit_memory_or_exit(char* addr, size_t size, 3184Srgrimes size_t alignment_hint, 3194Srgrimes bool executable, const char* mesg); 3204Srgrimes static bool uncommit_memory(char* addr, size_t bytes); 3214Srgrimes static bool release_memory(char* addr, size_t bytes); 3224Srgrimes 3234Srgrimes enum ProtType { MEM_PROT_NONE, MEM_PROT_READ, MEM_PROT_RW, MEM_PROT_RWX }; 3244Srgrimes static bool protect_memory(char* addr, size_t bytes, ProtType prot, 3254Srgrimes bool is_committed = true); 3264Srgrimes 3274Srgrimes static bool guard_memory(char* addr, size_t bytes); 328798Swollman static bool unguard_memory(char* addr, size_t bytes); 3294Srgrimes static bool create_stack_guard_pages(char* addr, size_t bytes); 330798Swollman static bool pd_create_stack_guard_pages(char* addr, size_t bytes); 3314Srgrimes static bool remove_stack_guard_pages(char* addr, size_t bytes); 3328876Srgrimes 333 static char* map_memory(int fd, const char* file_name, size_t file_offset, 334 char *addr, size_t bytes, bool read_only = false, 335 bool allow_exec = false); 336 static char* remap_memory(int fd, const char* file_name, size_t file_offset, 337 char *addr, size_t bytes, bool read_only, 338 bool allow_exec); 339 static bool unmap_memory(char *addr, size_t bytes); 340 static void free_memory(char *addr, size_t bytes, size_t alignment_hint); 341 static void realign_memory(char *addr, size_t bytes, size_t alignment_hint); 342 343 // NUMA-specific interface 344 static bool numa_has_static_binding(); 345 static bool numa_has_group_homing(); 346 static void numa_make_local(char *addr, size_t bytes, int lgrp_hint); 347 static void numa_make_global(char *addr, size_t bytes); 348 static size_t numa_get_groups_num(); 349 static size_t numa_get_leaf_groups(int *ids, size_t size); 350 static bool numa_topology_changed(); 351 static int numa_get_group_id(); 352 353 // Page manipulation 354 struct page_info { 355 size_t size; 356 int lgrp_id; 357 }; 358 static bool get_page_info(char *start, page_info* info); 359 static char* scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found); 360 361 static char* non_memory_address_word(); 362 // reserve, commit and pin the entire memory region 363 static char* reserve_memory_special(size_t size, size_t alignment, 364 char* addr, bool executable); 365 static bool release_memory_special(char* addr, size_t bytes); 366 static void large_page_init(); 367 static size_t large_page_size(); 368 static bool can_commit_large_page_memory(); 369 static bool can_execute_large_page_memory(); 370 371 // OS interface to polling page 372 static address get_polling_page() { return _polling_page; } 373 static void set_polling_page(address page) { _polling_page = page; } 374 static bool is_poll_address(address addr) { return addr >= _polling_page && addr < (_polling_page + os::vm_page_size()); } 375 static void make_polling_page_unreadable(); 376 static void make_polling_page_readable(); 377 378 // Routines used to serialize the thread state without using membars 379 static void serialize_thread_states(); 380 381 // Since we write to the serialize page from every thread, we 382 // want stores to be on unique cache lines whenever possible 383 // in order to minimize CPU cross talk. We pre-compute the 384 // amount to shift the thread* to make this offset unique to 385 // each thread. 386 static int get_serialize_page_shift_count() { 387 return SerializePageShiftCount; 388 } 389 390 static void set_serialize_page_mask(uintptr_t mask) { 391 _serialize_page_mask = mask; 392 } 393 394 static unsigned int get_serialize_page_mask() { 395 return _serialize_page_mask; 396 } 397 398 static void set_memory_serialize_page(address page); 399 400 static address get_memory_serialize_page() { 401 return (address)_mem_serialize_page; 402 } 403 404 static inline void write_memory_serialize_page(JavaThread *thread) { 405 uintptr_t page_offset = ((uintptr_t)thread >> 406 get_serialize_page_shift_count()) & 407 get_serialize_page_mask(); 408 *(volatile int32_t *)((uintptr_t)_mem_serialize_page+page_offset) = 1; 409 } 410 411 static bool is_memory_serialize_page(JavaThread *thread, address addr) { 412 if (UseMembar) return false; 413 // Previously this function calculated the exact address of this 414 // thread's serialize page, and checked if the faulting address 415 // was equal. However, some platforms mask off faulting addresses 416 // to the page size, so now we just check that the address is 417 // within the page. This makes the thread argument unnecessary, 418 // but we retain the NULL check to preserve existing behavior. 419 if (thread == NULL) return false; 420 address page = (address) _mem_serialize_page; 421 return addr >= page && addr < (page + os::vm_page_size()); 422 } 423 424 static void block_on_serialize_page_trap(); 425 426 // threads 427 428 enum ThreadType { 429 vm_thread, 430 cgc_thread, // Concurrent GC thread 431 pgc_thread, // Parallel GC thread 432 java_thread, 433 compiler_thread, 434 watcher_thread, 435 os_thread 436 }; 437 438 static bool create_thread(Thread* thread, 439 ThreadType thr_type, 440 size_t stack_size = 0); 441 static bool create_main_thread(JavaThread* thread); 442 static bool create_attached_thread(JavaThread* thread); 443 static void pd_start_thread(Thread* thread); 444 static void start_thread(Thread* thread); 445 446 static void initialize_thread(Thread* thr); 447 static void free_thread(OSThread* osthread); 448 449 // thread id on Linux/64bit is 64bit, on Windows and Solaris, it's 32bit 450 static intx current_thread_id(); 451 static int current_process_id(); 452 static int sleep(Thread* thread, jlong ms, bool interruptable); 453 // Short standalone OS sleep suitable for slow path spin loop. 454 // Ignores Thread.interrupt() (so keep it short). 455 // ms = 0, will sleep for the least amount of time allowed by the OS. 456 static void naked_short_sleep(jlong ms); 457 static void infinite_sleep(); // never returns, use with CAUTION 458 static void naked_yield () ; 459 static OSReturn set_priority(Thread* thread, ThreadPriority priority); 460 static OSReturn get_priority(const Thread* const thread, ThreadPriority& priority); 461 462 static void interrupt(Thread* thread); 463 static bool is_interrupted(Thread* thread, bool clear_interrupted); 464 465 static int pd_self_suspend_thread(Thread* thread); 466 467 static ExtendedPC fetch_frame_from_context(void* ucVoid, intptr_t** sp, intptr_t** fp); 468 static frame fetch_frame_from_context(void* ucVoid); 469 470 static ExtendedPC get_thread_pc(Thread *thread); 471 static void breakpoint(); 472 473 static address current_stack_pointer(); 474 static address current_stack_base(); 475 static size_t current_stack_size(); 476 477 static void verify_stack_alignment() PRODUCT_RETURN; 478 479 static int message_box(const char* title, const char* message); 480 static char* do_you_want_to_debug(const char* message); 481 482 // run cmd in a separate process and return its exit code; or -1 on failures 483 static int fork_and_exec(char *cmd); 484 485 // Call ::exit() on all platforms but Windows 486 static void exit(int num); 487 488 // Terminate the VM, but don't exit the process 489 static void shutdown(); 490 491 // Terminate with an error. Default is to generate a core file on platforms 492 // that support such things. This calls shutdown() and then aborts. 493 static void abort(bool dump_core = true); 494 495 // Die immediately, no exit hook, no abort hook, no cleanup. 496 static void die(); 497 498 // File i/o operations 499 static const int default_file_open_flags(); 500 static int open(const char *path, int oflag, int mode); 501 static FILE* open(int fd, const char* mode); 502 static int close(int fd); 503 static jlong lseek(int fd, jlong offset, int whence); 504 static char* native_path(char *path); 505 static int ftruncate(int fd, jlong length); 506 static int fsync(int fd); 507 static int available(int fd, jlong *bytes); 508 509 //File i/o operations 510 511 static size_t read(int fd, void *buf, unsigned int nBytes); 512 static size_t restartable_read(int fd, void *buf, unsigned int nBytes); 513 static size_t write(int fd, const void *buf, unsigned int nBytes); 514 515 // Reading directories. 516 static DIR* opendir(const char* dirname); 517 static int readdir_buf_size(const char *path); 518 static struct dirent* readdir(DIR* dirp, dirent* dbuf); 519 static int closedir(DIR* dirp); 520 521 // Dynamic library extension 522 static const char* dll_file_extension(); 523 524 static const char* get_temp_directory(); 525 static const char* get_current_directory(char *buf, size_t buflen); 526 527 // Builds a platform-specific full library path given a ld path and lib name 528 // Returns true if buffer contains full path to existing file, false otherwise 529 static bool dll_build_name(char* buffer, size_t size, 530 const char* pathname, const char* fname); 531 532 // Symbol lookup, find nearest function name; basically it implements 533 // dladdr() for all platforms. Name of the nearest function is copied 534 // to buf. Distance from its base address is optionally returned as offset. 535 // If function name is not found, buf[0] is set to '\0' and offset is 536 // set to -1 (if offset is non-NULL). 537 static bool dll_address_to_function_name(address addr, char* buf, 538 int buflen, int* offset); 539 540 // Locate DLL/DSO. On success, full path of the library is copied to 541 // buf, and offset is optionally set to be the distance between addr 542 // and the library's base address. On failure, buf[0] is set to '\0' 543 // and offset is set to -1 (if offset is non-NULL). 544 static bool dll_address_to_library_name(address addr, char* buf, 545 int buflen, int* offset); 546 547 // Find out whether the pc is in the static code for jvm.dll/libjvm.so. 548 static bool address_is_in_vm(address addr); 549 550 // Loads .dll/.so and 551 // in case of error it checks if .dll/.so was built for the 552 // same architecture as HotSpot is running on 553 static void* dll_load(const char *name, char *ebuf, int ebuflen); 554 555 // lookup symbol in a shared library 556 static void* dll_lookup(void* handle, const char* name); 557 558 // Unload library 559 static void dll_unload(void *lib); 560 561 // Callback for loaded module information 562 // Input parameters: 563 // char* module_file_name, 564 // address module_base_addr, 565 // address module_top_addr, 566 // void* param 567 typedef int (*LoadedModulesCallbackFunc)(const char *, address, address, void *); 568 569 static int get_loaded_modules_info(LoadedModulesCallbackFunc callback, void *param); 570 571 // Return the handle of this process 572 static void* get_default_process_handle(); 573 574 // Check for static linked agent library 575 static bool find_builtin_agent(AgentLibrary *agent_lib, const char *syms[], 576 size_t syms_len); 577 578 // Find agent entry point 579 static void *find_agent_function(AgentLibrary *agent_lib, bool check_lib, 580 const char *syms[], size_t syms_len); 581 582 // Print out system information; they are called by fatal error handler. 583 // Output format may be different on different platforms. 584 static void print_os_info(outputStream* st); 585 static void print_os_info_brief(outputStream* st); 586 static void print_cpu_info(outputStream* st); 587 static void pd_print_cpu_info(outputStream* st); 588 static void print_memory_info(outputStream* st); 589 static void print_dll_info(outputStream* st); 590 static void print_environment_variables(outputStream* st, const char** env_list, char* buffer, int len); 591 static void print_context(outputStream* st, void* context); 592 static void print_register_info(outputStream* st, void* context); 593 static void print_siginfo(outputStream* st, void* siginfo); 594 static void print_signal_handlers(outputStream* st, char* buf, size_t buflen); 595 static void print_date_and_time(outputStream* st); 596 597 static void print_location(outputStream* st, intptr_t x, bool verbose = false); 598 static size_t lasterror(char *buf, size_t len); 599 static int get_last_error(); 600 601 // Determines whether the calling process is being debugged by a user-mode debugger. 602 static bool is_debugger_attached(); 603 604 // wait for a key press if PauseAtExit is set 605 static void wait_for_keypress_at_exit(void); 606 607 // The following two functions are used by fatal error handler to trace 608 // native (C) frames. They are not part of frame.hpp/frame.cpp because 609 // frame.hpp/cpp assume thread is JavaThread, and also because different 610 // OS/compiler may have different convention or provide different API to 611 // walk C frames. 612 // 613 // We don't attempt to become a debugger, so we only follow frames if that 614 // does not require a lookup in the unwind table, which is part of the binary 615 // file but may be unsafe to read after a fatal error. So on x86, we can 616 // only walk stack if %ebp is used as frame pointer; on ia64, it's not 617 // possible to walk C stack without having the unwind table. 618 static bool is_first_C_frame(frame *fr); 619 static frame get_sender_for_C_frame(frame *fr); 620 621 // return current frame. pc() and sp() are set to NULL on failure. 622 static frame current_frame(); 623 624 static void print_hex_dump(outputStream* st, address start, address end, int unitsize); 625 626 // returns a string to describe the exception/signal; 627 // returns NULL if exception_code is not an OS exception/signal. 628 static const char* exception_name(int exception_code, char* buf, size_t buflen); 629 630 // Returns native Java library, loads if necessary 631 static void* native_java_library(); 632 633 // Fills in path to jvm.dll/libjvm.so (used by the Disassembler) 634 static void jvm_path(char *buf, jint buflen); 635 636 // Returns true if we are running in a headless jre. 637 static bool is_headless_jre(); 638 639 // JNI names 640 static void print_jni_name_prefix_on(outputStream* st, int args_size); 641 static void print_jni_name_suffix_on(outputStream* st, int args_size); 642 643 // Init os specific system properties values 644 static void init_system_properties_values(); 645 646 // IO operations, non-JVM_ version. 647 static int stat(const char* path, struct stat* sbuf); 648 static bool dir_is_empty(const char* path); 649 650 // IO operations on binary files 651 static int create_binary_file(const char* path, bool rewrite_existing); 652 static jlong current_file_offset(int fd); 653 static jlong seek_to_file_offset(int fd, jlong offset); 654 655 // Thread Local Storage 656 static int allocate_thread_local_storage(); 657 static void thread_local_storage_at_put(int index, void* value); 658 static void* thread_local_storage_at(int index); 659 static void free_thread_local_storage(int index); 660 661 // Retrieve native stack frames. 662 // Parameter: 663 // stack: an array to storage stack pointers. 664 // frames: size of above array. 665 // toSkip: number of stack frames to skip at the beginning. 666 // Return: number of stack frames captured. 667 static int get_native_stack(address* stack, int size, int toSkip = 0); 668 669 // General allocation (must be MT-safe) 670 static void* malloc (size_t size, MEMFLAGS flags, const NativeCallStack& stack); 671 static void* malloc (size_t size, MEMFLAGS flags); 672 static void* realloc (void *memblock, size_t size, MEMFLAGS flag, const NativeCallStack& stack); 673 static void* realloc (void *memblock, size_t size, MEMFLAGS flag); 674 675 static void free (void *memblock, MEMFLAGS flags = mtNone); 676 static bool check_heap(bool force = false); // verify C heap integrity 677 static char* strdup(const char *, MEMFLAGS flags = mtInternal); // Like strdup 678 // Like strdup, but exit VM when strdup() returns NULL 679 static char* strdup_check_oom(const char*, MEMFLAGS flags = mtInternal); 680 681#ifndef PRODUCT 682 static julong num_mallocs; // # of calls to malloc/realloc 683 static julong alloc_bytes; // # of bytes allocated 684 static julong num_frees; // # of calls to free 685 static julong free_bytes; // # of bytes freed 686#endif 687 688 // SocketInterface (ex HPI SocketInterface ) 689 static int socket(int domain, int type, int protocol); 690 static int socket_close(int fd); 691 static int socket_shutdown(int fd, int howto); 692 static int recv(int fd, char* buf, size_t nBytes, uint flags); 693 static int send(int fd, char* buf, size_t nBytes, uint flags); 694 static int raw_send(int fd, char* buf, size_t nBytes, uint flags); 695 static int timeout(int fd, long timeout); 696 static int listen(int fd, int count); 697 static int connect(int fd, struct sockaddr* him, socklen_t len); 698 static int bind(int fd, struct sockaddr* him, socklen_t len); 699 static int accept(int fd, struct sockaddr* him, socklen_t* len); 700 static int recvfrom(int fd, char* buf, size_t nbytes, uint flags, 701 struct sockaddr* from, socklen_t* fromlen); 702 static int get_sock_name(int fd, struct sockaddr* him, socklen_t* len); 703 static int sendto(int fd, char* buf, size_t len, uint flags, 704 struct sockaddr* to, socklen_t tolen); 705 static int socket_available(int fd, jint* pbytes); 706 707 static int get_sock_opt(int fd, int level, int optname, 708 char* optval, socklen_t* optlen); 709 static int set_sock_opt(int fd, int level, int optname, 710 const char* optval, socklen_t optlen); 711 static int get_host_name(char* name, int namelen); 712 713 static struct hostent* get_host_by_name(char* name); 714 715 // Support for signals (see JVM_RaiseSignal, JVM_RegisterSignal) 716 static void signal_init(); 717 static void signal_init_pd(); 718 static void signal_notify(int signal_number); 719 static void* signal(int signal_number, void* handler); 720 static void signal_raise(int signal_number); 721 static int signal_wait(); 722 static int signal_lookup(); 723 static void* user_handler(); 724 static void terminate_signal_thread(); 725 static int sigexitnum_pd(); 726 727 // random number generation 728 static long random(); // return 32bit pseudorandom number 729 static void init_random(long initval); // initialize random sequence 730 731 // Structured OS Exception support 732 static void os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method, JavaCallArguments* args, Thread* thread); 733 734 // On Windows this will create an actual minidump, on Linux/Solaris it will simply check core dump limits 735 static void check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize); 736 737 // Get the default path to the core file 738 // Returns the length of the string 739 static int get_core_path(char* buffer, size_t bufferSize); 740 741 // JVMTI & JVM monitoring and management support 742 // The thread_cpu_time() and current_thread_cpu_time() are only 743 // supported if is_thread_cpu_time_supported() returns true. 744 // They are not supported on Solaris T1. 745 746 // Thread CPU Time - return the fast estimate on a platform 747 // On Solaris - call gethrvtime (fast) - user time only 748 // On Linux - fast clock_gettime where available - user+sys 749 // - otherwise: very slow /proc fs - user+sys 750 // On Windows - GetThreadTimes - user+sys 751 static jlong current_thread_cpu_time(); 752 static jlong thread_cpu_time(Thread* t); 753 754 // Thread CPU Time with user_sys_cpu_time parameter. 755 // 756 // If user_sys_cpu_time is true, user+sys time is returned. 757 // Otherwise, only user time is returned 758 static jlong current_thread_cpu_time(bool user_sys_cpu_time); 759 static jlong thread_cpu_time(Thread* t, bool user_sys_cpu_time); 760 761 // Return a bunch of info about the timers. 762 // Note that the returned info for these two functions may be different 763 // on some platforms 764 static void current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr); 765 static void thread_cpu_time_info(jvmtiTimerInfo *info_ptr); 766 767 static bool is_thread_cpu_time_supported(); 768 769 // System loadavg support. Returns -1 if load average cannot be obtained. 770 static int loadavg(double loadavg[], int nelem); 771 772 // Hook for os specific jvm options that we don't want to abort on seeing 773 static bool obsolete_option(const JavaVMOption *option); 774 775 // Extensions 776#include "runtime/os_ext.hpp" 777 778 public: 779 class CrashProtectionCallback : public StackObj { 780 public: 781 virtual void call() = 0; 782 }; 783 784 // Platform dependent stuff 785#ifdef TARGET_OS_FAMILY_linux 786# include "os_linux.hpp" 787# include "os_posix.hpp" 788#endif 789#ifdef TARGET_OS_FAMILY_solaris 790# include "os_solaris.hpp" 791# include "os_posix.hpp" 792#endif 793#ifdef TARGET_OS_FAMILY_windows 794# include "os_windows.hpp" 795#endif 796#ifdef TARGET_OS_FAMILY_aix 797# include "os_aix.hpp" 798# include "os_posix.hpp" 799#endif 800#ifdef TARGET_OS_FAMILY_bsd 801# include "os_posix.hpp" 802# include "os_bsd.hpp" 803#endif 804#ifdef TARGET_OS_ARCH_linux_x86 805# include "os_linux_x86.hpp" 806#endif 807#ifdef TARGET_OS_ARCH_linux_sparc 808# include "os_linux_sparc.hpp" 809#endif 810#ifdef TARGET_OS_ARCH_linux_zero 811# include "os_linux_zero.hpp" 812#endif 813#ifdef TARGET_OS_ARCH_solaris_x86 814# include "os_solaris_x86.hpp" 815#endif 816#ifdef TARGET_OS_ARCH_solaris_sparc 817# include "os_solaris_sparc.hpp" 818#endif 819#ifdef TARGET_OS_ARCH_windows_x86 820# include "os_windows_x86.hpp" 821#endif 822#ifdef TARGET_OS_ARCH_linux_arm 823# include "os_linux_arm.hpp" 824#endif 825#ifdef TARGET_OS_ARCH_linux_ppc 826# include "os_linux_ppc.hpp" 827#endif 828#ifdef TARGET_OS_ARCH_aix_ppc 829# include "os_aix_ppc.hpp" 830#endif 831#ifdef TARGET_OS_ARCH_bsd_x86 832# include "os_bsd_x86.hpp" 833#endif 834#ifdef TARGET_OS_ARCH_bsd_zero 835# include "os_bsd_zero.hpp" 836#endif 837 838#ifndef OS_NATIVE_THREAD_CREATION_FAILED_MSG 839#define OS_NATIVE_THREAD_CREATION_FAILED_MSG "unable to create native thread: possibly out of memory or process/resource limits reached" 840#endif 841 842 public: 843#ifndef PLATFORM_PRINT_NATIVE_STACK 844 // No platform-specific code for printing the native stack. 845 static bool platform_print_native_stack(outputStream* st, void* context, 846 char *buf, int buf_size) { 847 return false; 848 } 849#endif 850 851 // debugging support (mostly used by debug.cpp but also fatal error handler) 852 static bool find(address pc, outputStream* st = tty); // OS specific function to make sense out of an address 853 854 static bool dont_yield(); // when true, JVM_Yield() is nop 855 static void print_statistics(); 856 857 // Thread priority helpers (implemented in OS-specific part) 858 static OSReturn set_native_priority(Thread* thread, int native_prio); 859 static OSReturn get_native_priority(const Thread* const thread, int* priority_ptr); 860 static int java_to_os_priority[CriticalPriority + 1]; 861 // Hint to the underlying OS that a task switch would not be good. 862 // Void return because it's a hint and can fail. 863 static void hint_no_preempt(); 864 static const char* native_thread_creation_failed_msg() { 865 return OS_NATIVE_THREAD_CREATION_FAILED_MSG; 866 } 867 868 // Used at creation if requested by the diagnostic flag PauseAtStartup. 869 // Causes the VM to wait until an external stimulus has been applied 870 // (for Unix, that stimulus is a signal, for Windows, an external 871 // ResumeThread call) 872 static void pause(); 873 874 // Builds a platform dependent Agent_OnLoad_<libname> function name 875 // which is used to find statically linked in agents. 876 static char* build_agent_function_name(const char *sym, const char *cname, 877 bool is_absolute_path); 878 879 class SuspendedThreadTaskContext { 880 public: 881 SuspendedThreadTaskContext(Thread* thread, void *ucontext) : _thread(thread), _ucontext(ucontext) {} 882 Thread* thread() const { return _thread; } 883 void* ucontext() const { return _ucontext; } 884 private: 885 Thread* _thread; 886 void* _ucontext; 887 }; 888 889 class SuspendedThreadTask { 890 public: 891 SuspendedThreadTask(Thread* thread) : _thread(thread), _done(false) {} 892 virtual ~SuspendedThreadTask() {} 893 void run(); 894 bool is_done() { return _done; } 895 virtual void do_task(const SuspendedThreadTaskContext& context) = 0; 896 protected: 897 private: 898 void internal_do_task(); 899 Thread* _thread; 900 bool _done; 901 }; 902 903#ifndef TARGET_OS_FAMILY_windows 904 // Suspend/resume support 905 // Protocol: 906 // 907 // a thread starts in SR_RUNNING 908 // 909 // SR_RUNNING can go to 910 // * SR_SUSPEND_REQUEST when the WatcherThread wants to suspend it 911 // SR_SUSPEND_REQUEST can go to 912 // * SR_RUNNING if WatcherThread decides it waited for SR_SUSPENDED too long (timeout) 913 // * SR_SUSPENDED if the stopped thread receives the signal and switches state 914 // SR_SUSPENDED can go to 915 // * SR_WAKEUP_REQUEST when the WatcherThread has done the work and wants to resume 916 // SR_WAKEUP_REQUEST can go to 917 // * SR_RUNNING when the stopped thread receives the signal 918 // * SR_WAKEUP_REQUEST on timeout (resend the signal and try again) 919 class SuspendResume { 920 public: 921 enum State { 922 SR_RUNNING, 923 SR_SUSPEND_REQUEST, 924 SR_SUSPENDED, 925 SR_WAKEUP_REQUEST 926 }; 927 928 private: 929 volatile State _state; 930 931 private: 932 /* try to switch state from state "from" to state "to" 933 * returns the state set after the method is complete 934 */ 935 State switch_state(State from, State to); 936 937 public: 938 SuspendResume() : _state(SR_RUNNING) { } 939 940 State state() const { return _state; } 941 942 State request_suspend() { 943 return switch_state(SR_RUNNING, SR_SUSPEND_REQUEST); 944 } 945 946 State cancel_suspend() { 947 return switch_state(SR_SUSPEND_REQUEST, SR_RUNNING); 948 } 949 950 State suspended() { 951 return switch_state(SR_SUSPEND_REQUEST, SR_SUSPENDED); 952 } 953 954 State request_wakeup() { 955 return switch_state(SR_SUSPENDED, SR_WAKEUP_REQUEST); 956 } 957 958 State running() { 959 return switch_state(SR_WAKEUP_REQUEST, SR_RUNNING); 960 } 961 962 bool is_running() const { 963 return _state == SR_RUNNING; 964 } 965 966 bool is_suspend_request() const { 967 return _state == SR_SUSPEND_REQUEST; 968 } 969 970 bool is_suspended() const { 971 return _state == SR_SUSPENDED; 972 } 973 }; 974#endif 975 976 977 protected: 978 static long _rand_seed; // seed for random number generator 979 static int _processor_count; // number of processors 980 981 static char* format_boot_path(const char* format_string, 982 const char* home, 983 int home_len, 984 char fileSep, 985 char pathSep); 986 static bool set_boot_path(char fileSep, char pathSep); 987 static char** split_path(const char* path, int* n); 988 989}; 990 991// Note that "PAUSE" is almost always used with synchronization 992// so arguably we should provide Atomic::SpinPause() instead 993// of the global SpinPause() with C linkage. 994// It'd also be eligible for inlining on many platforms. 995 996extern "C" int SpinPause(); 997 998#endif // SHARE_VM_RUNTIME_OS_HPP 999