1 /* 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 // no precompiled headers 26 #include "classfile/classLoader.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "code/icBuffer.hpp" 30 #include "code/vtableStubs.hpp" 31 #include "compiler/compileBroker.hpp" 32 #include "compiler/disassembler.hpp" 33 #include "interpreter/interpreter.hpp" 34 #include "jvm_linux.h" 35 #include "logging/log.hpp" 36 #include "memory/allocation.inline.hpp" 37 #include "memory/filemap.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "os_linux.inline.hpp" 40 #include "os_share_linux.hpp" 41 #include "prims/jniFastGetField.hpp" 42 #include "prims/jvm.h" 43 #include "prims/jvm_misc.hpp" 44 #include "runtime/arguments.hpp" 45 #include "runtime/atomic.hpp" 46 #include "runtime/extendedPC.hpp" 47 #include "runtime/globals.hpp" 48 #include "runtime/interfaceSupport.hpp" 49 #include "runtime/init.hpp" 50 #include "runtime/java.hpp" 51 #include "runtime/javaCalls.hpp" 52 #include "runtime/mutexLocker.hpp" 53 #include "runtime/objectMonitor.hpp" 54 #include "runtime/orderAccess.inline.hpp" 55 #include "runtime/osThread.hpp" 56 #include "runtime/perfMemory.hpp" 57 #include "runtime/sharedRuntime.hpp" 58 #include "runtime/statSampler.hpp" 59 #include "runtime/stubRoutines.hpp" 60 #include "runtime/thread.inline.hpp" 61 #include "runtime/threadCritical.hpp" 62 #include "runtime/timer.hpp" 63 #include "semaphore_posix.hpp" 64 #include "services/attachListener.hpp" 65 #include "services/memTracker.hpp" 66 #include "services/runtimeService.hpp" 67 #include "utilities/decoder.hpp" 68 #include "utilities/defaultStream.hpp" 69 #include "utilities/events.hpp" 70 #include "utilities/elfFile.hpp" 71 #include "utilities/growableArray.hpp" 72 #include "utilities/macros.hpp" 73 #include "utilities/vmError.hpp" 74 75 // put OS-includes here 76 # include <sys/types.h> 77 # include <sys/mman.h> 78 # include <sys/stat.h> 79 # include <sys/select.h> 80 # include <pthread.h> 81 # include <signal.h> 82 # include <errno.h> 83 # include <dlfcn.h> 84 # include <stdio.h> 85 # include <unistd.h> 86 # include <sys/resource.h> 87 # include <pthread.h> 88 # include <sys/stat.h> 89 # include <sys/time.h> 90 # include <sys/times.h> 91 # include <sys/utsname.h> 92 # include <sys/socket.h> 93 # include <sys/wait.h> 94 # include <pwd.h> 95 # include <poll.h> 96 # include <semaphore.h> 97 # include <fcntl.h> 98 # include <string.h> 99 # include <syscall.h> 100 # include <sys/sysinfo.h> 101 # include <sys/ipc.h> 102 # include <sys/shm.h> 103 # include <link.h> 104 # include <stdint.h> 105 # include <inttypes.h> 106 # include <sys/ioctl.h> 107 108 #ifndef _GNU_SOURCE 109 #define _GNU_SOURCE 110 #include <sched.h> 111 #undef _GNU_SOURCE 112 #else 113 #include <sched.h> 114 #endif 115 116 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling 117 // getrusage() is prepared to handle the associated failure. 118 #ifndef RUSAGE_THREAD 119 #define RUSAGE_THREAD (1) /* only the calling thread */ 120 #endif 121 122 #define MAX_PATH (2 * K) 123 124 #define MAX_SECS 100000000 125 126 // for timer info max values which include all bits 127 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 128 129 #define LARGEPAGES_BIT (1 << 6) 130 //////////////////////////////////////////////////////////////////////////////// 131 // global variables 132 julong os::Linux::_physical_memory = 0; 133 134 address os::Linux::_initial_thread_stack_bottom = NULL; 135 uintptr_t os::Linux::_initial_thread_stack_size = 0; 136 137 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL; 138 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL; 139 int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL; 140 Mutex* os::Linux::_createThread_lock = NULL; 141 pthread_t os::Linux::_main_thread; 142 int os::Linux::_page_size = -1; 143 bool os::Linux::_supports_fast_thread_cpu_time = false; 144 uint32_t os::Linux::_os_version = 0; 145 const char * os::Linux::_glibc_version = "unknown"; 146 const char * os::Linux::_libpthread_version = "unknown"; 147 pthread_condattr_t os::Linux::_condattr[1]; 148 149 static jlong initial_time_count=0; 150 151 static int clock_tics_per_sec = 100; 152 153 // For diagnostics to print a message once. see run_periodic_checks 154 static sigset_t check_signal_done; 155 static bool check_signals = true; 156 157 // Signal number used to suspend/resume a thread 158 159 // do not use any signal number less than SIGSEGV, see 4355769 160 static int SR_signum = SIGUSR2; 161 sigset_t SR_sigset; 162 163 // Declarations 164 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time); 165 166 // utility functions 167 168 static int SR_initialize(); 169 170 julong os::available_memory() { 171 return Linux::available_memory(); 172 } 173 174 julong os::Linux::available_memory() { 175 // values in struct sysinfo are "unsigned long" 176 struct sysinfo si; 177 sysinfo(&si); 178 179 return (julong)si.freeram * si.mem_unit; 180 } 181 182 julong os::physical_memory() { 183 return Linux::physical_memory(); 184 } 185 186 // Return true if user is running as root. 187 188 bool os::have_special_privileges() { 189 static bool init = false; 190 static bool privileges = false; 191 if (!init) { 192 privileges = (getuid() != geteuid()) || (getgid() != getegid()); 193 init = true; 194 } 195 return privileges; 196 } 197 198 199 #ifndef SYS_gettid 200 // i386: 224, ia64: 1105, amd64: 186, sparc 143 201 #ifdef __ia64__ 202 #define SYS_gettid 1105 203 #else 204 #ifdef __i386__ 205 #define SYS_gettid 224 206 #else 207 #ifdef __amd64__ 208 #define SYS_gettid 186 209 #else 210 #ifdef __sparc__ 211 #define SYS_gettid 143 212 #else 213 #error define gettid for the arch 214 #endif 215 #endif 216 #endif 217 #endif 218 #endif 219 220 221 // pid_t gettid() 222 // 223 // Returns the kernel thread id of the currently running thread. Kernel 224 // thread id is used to access /proc. 225 pid_t os::Linux::gettid() { 226 int rslt = syscall(SYS_gettid); 227 assert(rslt != -1, "must be."); // old linuxthreads implementation? 228 return (pid_t)rslt; 229 } 230 231 // Most versions of linux have a bug where the number of processors are 232 // determined by looking at the /proc file system. In a chroot environment, 233 // the system call returns 1. This causes the VM to act as if it is 234 // a single processor and elide locking (see is_MP() call). 235 static bool unsafe_chroot_detected = false; 236 static const char *unstable_chroot_error = "/proc file system not found.\n" 237 "Java may be unstable running multithreaded in a chroot " 238 "environment on Linux when /proc filesystem is not mounted."; 239 240 void os::Linux::initialize_system_info() { 241 set_processor_count(sysconf(_SC_NPROCESSORS_CONF)); 242 if (processor_count() == 1) { 243 pid_t pid = os::Linux::gettid(); 244 char fname[32]; 245 jio_snprintf(fname, sizeof(fname), "/proc/%d", pid); 246 FILE *fp = fopen(fname, "r"); 247 if (fp == NULL) { 248 unsafe_chroot_detected = true; 249 } else { 250 fclose(fp); 251 } 252 } 253 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE); 254 assert(processor_count() > 0, "linux error"); 255 } 256 257 void os::init_system_properties_values() { 258 // The next steps are taken in the product version: 259 // 260 // Obtain the JAVA_HOME value from the location of libjvm.so. 261 // This library should be located at: 262 // <JAVA_HOME>/lib/{client|server}/libjvm.so. 263 // 264 // If "/jre/lib/" appears at the right place in the path, then we 265 // assume libjvm.so is installed in a JDK and we use this path. 266 // 267 // Otherwise exit with message: "Could not create the Java virtual machine." 268 // 269 // The following extra steps are taken in the debugging version: 270 // 271 // If "/jre/lib/" does NOT appear at the right place in the path 272 // instead of exit check for $JAVA_HOME environment variable. 273 // 274 // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>, 275 // then we append a fake suffix "hotspot/libjvm.so" to this path so 276 // it looks like libjvm.so is installed there 277 // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so. 278 // 279 // Otherwise exit. 280 // 281 // Important note: if the location of libjvm.so changes this 282 // code needs to be changed accordingly. 283 284 // See ld(1): 285 // The linker uses the following search paths to locate required 286 // shared libraries: 287 // 1: ... 288 // ... 289 // 7: The default directories, normally /lib and /usr/lib. 290 #if defined(AMD64) || (defined(_LP64) && defined(SPARC)) || defined(PPC64) || defined(S390) 291 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib" 292 #else 293 #define DEFAULT_LIBPATH "/lib:/usr/lib" 294 #endif 295 296 // Base path of extensions installed on the system. 297 #define SYS_EXT_DIR "/usr/java/packages" 298 #define EXTENSIONS_DIR "/lib/ext" 299 300 // Buffer that fits several sprintfs. 301 // Note that the space for the colon and the trailing null are provided 302 // by the nulls included by the sizeof operator. 303 const size_t bufsize = 304 MAX2((size_t)MAXPATHLEN, // For dll_dir & friends. 305 (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR)); // extensions dir 306 char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); 307 308 // sysclasspath, java_home, dll_dir 309 { 310 char *pslash; 311 os::jvm_path(buf, bufsize); 312 313 // Found the full path to libjvm.so. 314 // Now cut the path to <java_home>/jre if we can. 315 pslash = strrchr(buf, '/'); 316 if (pslash != NULL) { 317 *pslash = '\0'; // Get rid of /libjvm.so. 318 } 319 pslash = strrchr(buf, '/'); 320 if (pslash != NULL) { 321 *pslash = '\0'; // Get rid of /{client|server|hotspot}. 322 } 323 Arguments::set_dll_dir(buf); 324 325 if (pslash != NULL) { 326 pslash = strrchr(buf, '/'); 327 if (pslash != NULL) { 328 *pslash = '\0'; // Get rid of /lib. 329 } 330 } 331 Arguments::set_java_home(buf); 332 set_boot_path('/', ':'); 333 } 334 335 // Where to look for native libraries. 336 // 337 // Note: Due to a legacy implementation, most of the library path 338 // is set in the launcher. This was to accomodate linking restrictions 339 // on legacy Linux implementations (which are no longer supported). 340 // Eventually, all the library path setting will be done here. 341 // 342 // However, to prevent the proliferation of improperly built native 343 // libraries, the new path component /usr/java/packages is added here. 344 // Eventually, all the library path setting will be done here. 345 { 346 // Get the user setting of LD_LIBRARY_PATH, and prepended it. It 347 // should always exist (until the legacy problem cited above is 348 // addressed). 349 const char *v = ::getenv("LD_LIBRARY_PATH"); 350 const char *v_colon = ":"; 351 if (v == NULL) { v = ""; v_colon = ""; } 352 // That's +1 for the colon and +1 for the trailing '\0'. 353 char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, 354 strlen(v) + 1 + 355 sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1, 356 mtInternal); 357 sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon); 358 Arguments::set_library_path(ld_library_path); 359 FREE_C_HEAP_ARRAY(char, ld_library_path); 360 } 361 362 // Extensions directories. 363 sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home()); 364 Arguments::set_ext_dirs(buf); 365 366 FREE_C_HEAP_ARRAY(char, buf); 367 368 #undef DEFAULT_LIBPATH 369 #undef SYS_EXT_DIR 370 #undef EXTENSIONS_DIR 371 } 372 373 //////////////////////////////////////////////////////////////////////////////// 374 // breakpoint support 375 376 void os::breakpoint() { 377 BREAKPOINT; 378 } 379 380 extern "C" void breakpoint() { 381 // use debugger to set breakpoint here 382 } 383 384 //////////////////////////////////////////////////////////////////////////////// 385 // signal support 386 387 debug_only(static bool signal_sets_initialized = false); 388 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs; 389 390 bool os::Linux::is_sig_ignored(int sig) { 391 struct sigaction oact; 392 sigaction(sig, (struct sigaction*)NULL, &oact); 393 void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction) 394 : CAST_FROM_FN_PTR(void*, oact.sa_handler); 395 if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) { 396 return true; 397 } else { 398 return false; 399 } 400 } 401 402 void os::Linux::signal_sets_init() { 403 // Should also have an assertion stating we are still single-threaded. 404 assert(!signal_sets_initialized, "Already initialized"); 405 // Fill in signals that are necessarily unblocked for all threads in 406 // the VM. Currently, we unblock the following signals: 407 // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden 408 // by -Xrs (=ReduceSignalUsage)); 409 // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all 410 // other threads. The "ReduceSignalUsage" boolean tells us not to alter 411 // the dispositions or masks wrt these signals. 412 // Programs embedding the VM that want to use the above signals for their 413 // own purposes must, at this time, use the "-Xrs" option to prevent 414 // interference with shutdown hooks and BREAK_SIGNAL thread dumping. 415 // (See bug 4345157, and other related bugs). 416 // In reality, though, unblocking these signals is really a nop, since 417 // these signals are not blocked by default. 418 sigemptyset(&unblocked_sigs); 419 sigemptyset(&allowdebug_blocked_sigs); 420 sigaddset(&unblocked_sigs, SIGILL); 421 sigaddset(&unblocked_sigs, SIGSEGV); 422 sigaddset(&unblocked_sigs, SIGBUS); 423 sigaddset(&unblocked_sigs, SIGFPE); 424 #if defined(PPC64) 425 sigaddset(&unblocked_sigs, SIGTRAP); 426 #endif 427 sigaddset(&unblocked_sigs, SR_signum); 428 429 if (!ReduceSignalUsage) { 430 if (!os::Linux::is_sig_ignored(SHUTDOWN1_SIGNAL)) { 431 sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL); 432 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL); 433 } 434 if (!os::Linux::is_sig_ignored(SHUTDOWN2_SIGNAL)) { 435 sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL); 436 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL); 437 } 438 if (!os::Linux::is_sig_ignored(SHUTDOWN3_SIGNAL)) { 439 sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL); 440 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL); 441 } 442 } 443 // Fill in signals that are blocked by all but the VM thread. 444 sigemptyset(&vm_sigs); 445 if (!ReduceSignalUsage) { 446 sigaddset(&vm_sigs, BREAK_SIGNAL); 447 } 448 debug_only(signal_sets_initialized = true); 449 450 } 451 452 // These are signals that are unblocked while a thread is running Java. 453 // (For some reason, they get blocked by default.) 454 sigset_t* os::Linux::unblocked_signals() { 455 assert(signal_sets_initialized, "Not initialized"); 456 return &unblocked_sigs; 457 } 458 459 // These are the signals that are blocked while a (non-VM) thread is 460 // running Java. Only the VM thread handles these signals. 461 sigset_t* os::Linux::vm_signals() { 462 assert(signal_sets_initialized, "Not initialized"); 463 return &vm_sigs; 464 } 465 466 // These are signals that are blocked during cond_wait to allow debugger in 467 sigset_t* os::Linux::allowdebug_blocked_signals() { 468 assert(signal_sets_initialized, "Not initialized"); 469 return &allowdebug_blocked_sigs; 470 } 471 472 void os::Linux::hotspot_sigmask(Thread* thread) { 473 474 //Save caller's signal mask before setting VM signal mask 475 sigset_t caller_sigmask; 476 pthread_sigmask(SIG_BLOCK, NULL, &caller_sigmask); 477 478 OSThread* osthread = thread->osthread(); 479 osthread->set_caller_sigmask(caller_sigmask); 480 481 pthread_sigmask(SIG_UNBLOCK, os::Linux::unblocked_signals(), NULL); 482 483 if (!ReduceSignalUsage) { 484 if (thread->is_VM_thread()) { 485 // Only the VM thread handles BREAK_SIGNAL ... 486 pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL); 487 } else { 488 // ... all other threads block BREAK_SIGNAL 489 pthread_sigmask(SIG_BLOCK, vm_signals(), NULL); 490 } 491 } 492 } 493 494 ////////////////////////////////////////////////////////////////////////////// 495 // detecting pthread library 496 497 void os::Linux::libpthread_init() { 498 // Save glibc and pthread version strings. 499 #if !defined(_CS_GNU_LIBC_VERSION) || \ 500 !defined(_CS_GNU_LIBPTHREAD_VERSION) 501 #error "glibc too old (< 2.3.2)" 502 #endif 503 504 size_t n; 505 506 n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0); 507 if (n > 0) { 508 char* str = (char *)malloc(n, mtInternal); 509 confstr(_CS_GNU_LIBC_VERSION, str, n); 510 os::Linux::set_glibc_version(str); 511 } 512 513 n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0); 514 if (n > 0) { 515 char* str = (char *)malloc(n, mtInternal); 516 confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n); 517 os::Linux::set_libpthread_version(str); 518 } 519 } 520 521 ///////////////////////////////////////////////////////////////////////////// 522 // thread stack expansion 523 524 // os::Linux::manually_expand_stack() takes care of expanding the thread 525 // stack. Note that this is normally not needed: pthread stacks allocate 526 // thread stack using mmap() without MAP_NORESERVE, so the stack is already 527 // committed. Therefore it is not necessary to expand the stack manually. 528 // 529 // Manually expanding the stack was historically needed on LinuxThreads 530 // thread stacks, which were allocated with mmap(MAP_GROWSDOWN). Nowadays 531 // it is kept to deal with very rare corner cases: 532 // 533 // For one, user may run the VM on an own implementation of threads 534 // whose stacks are - like the old LinuxThreads - implemented using 535 // mmap(MAP_GROWSDOWN). 536 // 537 // Also, this coding may be needed if the VM is running on the primordial 538 // thread. Normally we avoid running on the primordial thread; however, 539 // user may still invoke the VM on the primordial thread. 540 // 541 // The following historical comment describes the details about running 542 // on a thread stack allocated with mmap(MAP_GROWSDOWN): 543 544 545 // Force Linux kernel to expand current thread stack. If "bottom" is close 546 // to the stack guard, caller should block all signals. 547 // 548 // MAP_GROWSDOWN: 549 // A special mmap() flag that is used to implement thread stacks. It tells 550 // kernel that the memory region should extend downwards when needed. This 551 // allows early versions of LinuxThreads to only mmap the first few pages 552 // when creating a new thread. Linux kernel will automatically expand thread 553 // stack as needed (on page faults). 554 // 555 // However, because the memory region of a MAP_GROWSDOWN stack can grow on 556 // demand, if a page fault happens outside an already mapped MAP_GROWSDOWN 557 // region, it's hard to tell if the fault is due to a legitimate stack 558 // access or because of reading/writing non-exist memory (e.g. buffer 559 // overrun). As a rule, if the fault happens below current stack pointer, 560 // Linux kernel does not expand stack, instead a SIGSEGV is sent to the 561 // application (see Linux kernel fault.c). 562 // 563 // This Linux feature can cause SIGSEGV when VM bangs thread stack for 564 // stack overflow detection. 565 // 566 // Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do 567 // not use MAP_GROWSDOWN. 568 // 569 // To get around the problem and allow stack banging on Linux, we need to 570 // manually expand thread stack after receiving the SIGSEGV. 571 // 572 // There are two ways to expand thread stack to address "bottom", we used 573 // both of them in JVM before 1.5: 574 // 1. adjust stack pointer first so that it is below "bottom", and then 575 // touch "bottom" 576 // 2. mmap() the page in question 577 // 578 // Now alternate signal stack is gone, it's harder to use 2. For instance, 579 // if current sp is already near the lower end of page 101, and we need to 580 // call mmap() to map page 100, it is possible that part of the mmap() frame 581 // will be placed in page 100. When page 100 is mapped, it is zero-filled. 582 // That will destroy the mmap() frame and cause VM to crash. 583 // 584 // The following code works by adjusting sp first, then accessing the "bottom" 585 // page to force a page fault. Linux kernel will then automatically expand the 586 // stack mapping. 587 // 588 // _expand_stack_to() assumes its frame size is less than page size, which 589 // should always be true if the function is not inlined. 590 591 static void NOINLINE _expand_stack_to(address bottom) { 592 address sp; 593 size_t size; 594 volatile char *p; 595 596 // Adjust bottom to point to the largest address within the same page, it 597 // gives us a one-page buffer if alloca() allocates slightly more memory. 598 bottom = (address)align_size_down((uintptr_t)bottom, os::Linux::page_size()); 599 bottom += os::Linux::page_size() - 1; 600 601 // sp might be slightly above current stack pointer; if that's the case, we 602 // will alloca() a little more space than necessary, which is OK. Don't use 603 // os::current_stack_pointer(), as its result can be slightly below current 604 // stack pointer, causing us to not alloca enough to reach "bottom". 605 sp = (address)&sp; 606 607 if (sp > bottom) { 608 size = sp - bottom; 609 p = (volatile char *)alloca(size); 610 assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?"); 611 p[0] = '\0'; 612 } 613 } 614 615 bool os::Linux::manually_expand_stack(JavaThread * t, address addr) { 616 assert(t!=NULL, "just checking"); 617 assert(t->osthread()->expanding_stack(), "expand should be set"); 618 assert(t->stack_base() != NULL, "stack_base was not initialized"); 619 620 if (addr < t->stack_base() && addr >= t->stack_reserved_zone_base()) { 621 sigset_t mask_all, old_sigset; 622 sigfillset(&mask_all); 623 pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset); 624 _expand_stack_to(addr); 625 pthread_sigmask(SIG_SETMASK, &old_sigset, NULL); 626 return true; 627 } 628 return false; 629 } 630 631 ////////////////////////////////////////////////////////////////////////////// 632 // create new thread 633 634 // Thread start routine for all newly created threads 635 static void *thread_native_entry(Thread *thread) { 636 // Try to randomize the cache line index of hot stack frames. 637 // This helps when threads of the same stack traces evict each other's 638 // cache lines. The threads can be either from the same JVM instance, or 639 // from different JVM instances. The benefit is especially true for 640 // processors with hyperthreading technology. 641 static int counter = 0; 642 int pid = os::current_process_id(); 643 alloca(((pid ^ counter++) & 7) * 128); 644 645 thread->initialize_thread_current(); 646 647 OSThread* osthread = thread->osthread(); 648 Monitor* sync = osthread->startThread_lock(); 649 650 osthread->set_thread_id(os::current_thread_id()); 651 652 log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").", 653 os::current_thread_id(), (uintx) pthread_self()); 654 655 if (UseNUMA) { 656 int lgrp_id = os::numa_get_group_id(); 657 if (lgrp_id != -1) { 658 thread->set_lgrp_id(lgrp_id); 659 } 660 } 661 // initialize signal mask for this thread 662 os::Linux::hotspot_sigmask(thread); 663 664 // initialize floating point control register 665 os::Linux::init_thread_fpu_state(); 666 667 // handshaking with parent thread 668 { 669 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag); 670 671 // notify parent thread 672 osthread->set_state(INITIALIZED); 673 sync->notify_all(); 674 675 // wait until os::start_thread() 676 while (osthread->get_state() == INITIALIZED) { 677 sync->wait(Mutex::_no_safepoint_check_flag); 678 } 679 } 680 681 // call one more level start routine 682 thread->run(); 683 684 log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").", 685 os::current_thread_id(), (uintx) pthread_self()); 686 687 // If a thread has not deleted itself ("delete this") as part of its 688 // termination sequence, we have to ensure thread-local-storage is 689 // cleared before we actually terminate. No threads should ever be 690 // deleted asynchronously with respect to their termination. 691 if (Thread::current_or_null_safe() != NULL) { 692 assert(Thread::current_or_null_safe() == thread, "current thread is wrong"); 693 thread->clear_thread_current(); 694 } 695 696 return 0; 697 } 698 699 bool os::create_thread(Thread* thread, ThreadType thr_type, 700 size_t req_stack_size) { 701 assert(thread->osthread() == NULL, "caller responsible"); 702 703 // Allocate the OSThread object 704 OSThread* osthread = new OSThread(NULL, NULL); 705 if (osthread == NULL) { 706 return false; 707 } 708 709 // set the correct thread state 710 osthread->set_thread_type(thr_type); 711 712 // Initial state is ALLOCATED but not INITIALIZED 713 osthread->set_state(ALLOCATED); 714 715 thread->set_osthread(osthread); 716 717 // init thread attributes 718 pthread_attr_t attr; 719 pthread_attr_init(&attr); 720 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 721 722 // Calculate stack size if it's not specified by caller. 723 size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size); 724 // In the Linux NPTL pthread implementation the guard size mechanism 725 // is not implemented properly. The posix standard requires adding 726 // the size of the guard pages to the stack size, instead Linux 727 // takes the space out of 'stacksize'. Thus we adapt the requested 728 // stack_size by the size of the guard pages to mimick proper 729 // behaviour. 730 stack_size = align_size_up(stack_size + os::Linux::default_guard_size(thr_type), vm_page_size()); 731 pthread_attr_setstacksize(&attr, stack_size); 732 733 // Configure glibc guard page. 734 pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type)); 735 736 ThreadState state; 737 738 { 739 pthread_t tid; 740 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread); 741 742 char buf[64]; 743 if (ret == 0) { 744 log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ", 745 (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr)); 746 } else { 747 log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.", 748 os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr)); 749 } 750 751 pthread_attr_destroy(&attr); 752 753 if (ret != 0) { 754 // Need to clean up stuff we've allocated so far 755 thread->set_osthread(NULL); 756 delete osthread; 757 return false; 758 } 759 760 // Store pthread info into the OSThread 761 osthread->set_pthread_id(tid); 762 763 // Wait until child thread is either initialized or aborted 764 { 765 Monitor* sync_with_child = osthread->startThread_lock(); 766 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag); 767 while ((state = osthread->get_state()) == ALLOCATED) { 768 sync_with_child->wait(Mutex::_no_safepoint_check_flag); 769 } 770 } 771 } 772 773 // Aborted due to thread limit being reached 774 if (state == ZOMBIE) { 775 thread->set_osthread(NULL); 776 delete osthread; 777 return false; 778 } 779 780 // The thread is returned suspended (in state INITIALIZED), 781 // and is started higher up in the call chain 782 assert(state == INITIALIZED, "race condition"); 783 return true; 784 } 785 786 ///////////////////////////////////////////////////////////////////////////// 787 // attach existing thread 788 789 // bootstrap the main thread 790 bool os::create_main_thread(JavaThread* thread) { 791 assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread"); 792 return create_attached_thread(thread); 793 } 794 795 bool os::create_attached_thread(JavaThread* thread) { 796 #ifdef ASSERT 797 thread->verify_not_published(); 798 #endif 799 800 // Allocate the OSThread object 801 OSThread* osthread = new OSThread(NULL, NULL); 802 803 if (osthread == NULL) { 804 return false; 805 } 806 807 // Store pthread info into the OSThread 808 osthread->set_thread_id(os::Linux::gettid()); 809 osthread->set_pthread_id(::pthread_self()); 810 811 // initialize floating point control register 812 os::Linux::init_thread_fpu_state(); 813 814 // Initial thread state is RUNNABLE 815 osthread->set_state(RUNNABLE); 816 817 thread->set_osthread(osthread); 818 819 if (UseNUMA) { 820 int lgrp_id = os::numa_get_group_id(); 821 if (lgrp_id != -1) { 822 thread->set_lgrp_id(lgrp_id); 823 } 824 } 825 826 if (os::Linux::is_initial_thread()) { 827 // If current thread is initial thread, its stack is mapped on demand, 828 // see notes about MAP_GROWSDOWN. Here we try to force kernel to map 829 // the entire stack region to avoid SEGV in stack banging. 830 // It is also useful to get around the heap-stack-gap problem on SuSE 831 // kernel (see 4821821 for details). We first expand stack to the top 832 // of yellow zone, then enable stack yellow zone (order is significant, 833 // enabling yellow zone first will crash JVM on SuSE Linux), so there 834 // is no gap between the last two virtual memory regions. 835 836 JavaThread *jt = (JavaThread *)thread; 837 address addr = jt->stack_reserved_zone_base(); 838 assert(addr != NULL, "initialization problem?"); 839 assert(jt->stack_available(addr) > 0, "stack guard should not be enabled"); 840 841 osthread->set_expanding_stack(); 842 os::Linux::manually_expand_stack(jt, addr); 843 osthread->clear_expanding_stack(); 844 } 845 846 // initialize signal mask for this thread 847 // and save the caller's signal mask 848 os::Linux::hotspot_sigmask(thread); 849 850 log_info(os, thread)("Thread attached (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").", 851 os::current_thread_id(), (uintx) pthread_self()); 852 853 return true; 854 } 855 856 void os::pd_start_thread(Thread* thread) { 857 OSThread * osthread = thread->osthread(); 858 assert(osthread->get_state() != INITIALIZED, "just checking"); 859 Monitor* sync_with_child = osthread->startThread_lock(); 860 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag); 861 sync_with_child->notify(); 862 } 863 864 // Free Linux resources related to the OSThread 865 void os::free_thread(OSThread* osthread) { 866 assert(osthread != NULL, "osthread not set"); 867 868 // We are told to free resources of the argument thread, 869 // but we can only really operate on the current thread. 870 assert(Thread::current()->osthread() == osthread, 871 "os::free_thread but not current thread"); 872 873 #ifdef ASSERT 874 sigset_t current; 875 sigemptyset(¤t); 876 pthread_sigmask(SIG_SETMASK, NULL, ¤t); 877 assert(!sigismember(¤t, SR_signum), "SR signal should not be blocked!"); 878 #endif 879 880 // Restore caller's signal mask 881 sigset_t sigmask = osthread->caller_sigmask(); 882 pthread_sigmask(SIG_SETMASK, &sigmask, NULL); 883 884 delete osthread; 885 } 886 887 ////////////////////////////////////////////////////////////////////////////// 888 // initial thread 889 890 // Check if current thread is the initial thread, similar to Solaris thr_main. 891 bool os::Linux::is_initial_thread(void) { 892 char dummy; 893 // If called before init complete, thread stack bottom will be null. 894 // Can be called if fatal error occurs before initialization. 895 if (initial_thread_stack_bottom() == NULL) return false; 896 assert(initial_thread_stack_bottom() != NULL && 897 initial_thread_stack_size() != 0, 898 "os::init did not locate initial thread's stack region"); 899 if ((address)&dummy >= initial_thread_stack_bottom() && 900 (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size()) { 901 return true; 902 } else { 903 return false; 904 } 905 } 906 907 // Find the virtual memory area that contains addr 908 static bool find_vma(address addr, address* vma_low, address* vma_high) { 909 FILE *fp = fopen("/proc/self/maps", "r"); 910 if (fp) { 911 address low, high; 912 while (!feof(fp)) { 913 if (fscanf(fp, "%p-%p", &low, &high) == 2) { 914 if (low <= addr && addr < high) { 915 if (vma_low) *vma_low = low; 916 if (vma_high) *vma_high = high; 917 fclose(fp); 918 return true; 919 } 920 } 921 for (;;) { 922 int ch = fgetc(fp); 923 if (ch == EOF || ch == (int)'\n') break; 924 } 925 } 926 fclose(fp); 927 } 928 return false; 929 } 930 931 // Locate initial thread stack. This special handling of initial thread stack 932 // is needed because pthread_getattr_np() on most (all?) Linux distros returns 933 // bogus value for the primordial process thread. While the launcher has created 934 // the VM in a new thread since JDK 6, we still have to allow for the use of the 935 // JNI invocation API from a primordial thread. 936 void os::Linux::capture_initial_stack(size_t max_size) { 937 938 // max_size is either 0 (which means accept OS default for thread stacks) or 939 // a user-specified value known to be at least the minimum needed. If we 940 // are actually on the primordial thread we can make it appear that we have a 941 // smaller max_size stack by inserting the guard pages at that location. But we 942 // cannot do anything to emulate a larger stack than what has been provided by 943 // the OS or threading library. In fact if we try to use a stack greater than 944 // what is set by rlimit then we will crash the hosting process. 945 946 // Maximum stack size is the easy part, get it from RLIMIT_STACK. 947 // If this is "unlimited" then it will be a huge value. 948 struct rlimit rlim; 949 getrlimit(RLIMIT_STACK, &rlim); 950 size_t stack_size = rlim.rlim_cur; 951 952 // 6308388: a bug in ld.so will relocate its own .data section to the 953 // lower end of primordial stack; reduce ulimit -s value a little bit 954 // so we won't install guard page on ld.so's data section. 955 stack_size -= 2 * page_size(); 956 957 // Try to figure out where the stack base (top) is. This is harder. 958 // 959 // When an application is started, glibc saves the initial stack pointer in 960 // a global variable "__libc_stack_end", which is then used by system 961 // libraries. __libc_stack_end should be pretty close to stack top. The 962 // variable is available since the very early days. However, because it is 963 // a private interface, it could disappear in the future. 964 // 965 // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar 966 // to __libc_stack_end, it is very close to stack top, but isn't the real 967 // stack top. Note that /proc may not exist if VM is running as a chroot 968 // program, so reading /proc/<pid>/stat could fail. Also the contents of 969 // /proc/<pid>/stat could change in the future (though unlikely). 970 // 971 // We try __libc_stack_end first. If that doesn't work, look for 972 // /proc/<pid>/stat. If neither of them works, we use current stack pointer 973 // as a hint, which should work well in most cases. 974 975 uintptr_t stack_start; 976 977 // try __libc_stack_end first 978 uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end"); 979 if (p && *p) { 980 stack_start = *p; 981 } else { 982 // see if we can get the start_stack field from /proc/self/stat 983 FILE *fp; 984 int pid; 985 char state; 986 int ppid; 987 int pgrp; 988 int session; 989 int nr; 990 int tpgrp; 991 unsigned long flags; 992 unsigned long minflt; 993 unsigned long cminflt; 994 unsigned long majflt; 995 unsigned long cmajflt; 996 unsigned long utime; 997 unsigned long stime; 998 long cutime; 999 long cstime; 1000 long prio; 1001 long nice; 1002 long junk; 1003 long it_real; 1004 uintptr_t start; 1005 uintptr_t vsize; 1006 intptr_t rss; 1007 uintptr_t rsslim; 1008 uintptr_t scodes; 1009 uintptr_t ecode; 1010 int i; 1011 1012 // Figure what the primordial thread stack base is. Code is inspired 1013 // by email from Hans Boehm. /proc/self/stat begins with current pid, 1014 // followed by command name surrounded by parentheses, state, etc. 1015 char stat[2048]; 1016 int statlen; 1017 1018 fp = fopen("/proc/self/stat", "r"); 1019 if (fp) { 1020 statlen = fread(stat, 1, 2047, fp); 1021 stat[statlen] = '\0'; 1022 fclose(fp); 1023 1024 // Skip pid and the command string. Note that we could be dealing with 1025 // weird command names, e.g. user could decide to rename java launcher 1026 // to "java 1.4.2 :)", then the stat file would look like 1027 // 1234 (java 1.4.2 :)) R ... ... 1028 // We don't really need to know the command string, just find the last 1029 // occurrence of ")" and then start parsing from there. See bug 4726580. 1030 char * s = strrchr(stat, ')'); 1031 1032 i = 0; 1033 if (s) { 1034 // Skip blank chars 1035 do { s++; } while (s && isspace(*s)); 1036 1037 #define _UFM UINTX_FORMAT 1038 #define _DFM INTX_FORMAT 1039 1040 // 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1041 // 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1042 i = sscanf(s, "%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld " _UFM _UFM _DFM _UFM _UFM _UFM _UFM, 1043 &state, // 3 %c 1044 &ppid, // 4 %d 1045 &pgrp, // 5 %d 1046 &session, // 6 %d 1047 &nr, // 7 %d 1048 &tpgrp, // 8 %d 1049 &flags, // 9 %lu 1050 &minflt, // 10 %lu 1051 &cminflt, // 11 %lu 1052 &majflt, // 12 %lu 1053 &cmajflt, // 13 %lu 1054 &utime, // 14 %lu 1055 &stime, // 15 %lu 1056 &cutime, // 16 %ld 1057 &cstime, // 17 %ld 1058 &prio, // 18 %ld 1059 &nice, // 19 %ld 1060 &junk, // 20 %ld 1061 &it_real, // 21 %ld 1062 &start, // 22 UINTX_FORMAT 1063 &vsize, // 23 UINTX_FORMAT 1064 &rss, // 24 INTX_FORMAT 1065 &rsslim, // 25 UINTX_FORMAT 1066 &scodes, // 26 UINTX_FORMAT 1067 &ecode, // 27 UINTX_FORMAT 1068 &stack_start); // 28 UINTX_FORMAT 1069 } 1070 1071 #undef _UFM 1072 #undef _DFM 1073 1074 if (i != 28 - 2) { 1075 assert(false, "Bad conversion from /proc/self/stat"); 1076 // product mode - assume we are the initial thread, good luck in the 1077 // embedded case. 1078 warning("Can't detect initial thread stack location - bad conversion"); 1079 stack_start = (uintptr_t) &rlim; 1080 } 1081 } else { 1082 // For some reason we can't open /proc/self/stat (for example, running on 1083 // FreeBSD with a Linux emulator, or inside chroot), this should work for 1084 // most cases, so don't abort: 1085 warning("Can't detect initial thread stack location - no /proc/self/stat"); 1086 stack_start = (uintptr_t) &rlim; 1087 } 1088 } 1089 1090 // Now we have a pointer (stack_start) very close to the stack top, the 1091 // next thing to do is to figure out the exact location of stack top. We 1092 // can find out the virtual memory area that contains stack_start by 1093 // reading /proc/self/maps, it should be the last vma in /proc/self/maps, 1094 // and its upper limit is the real stack top. (again, this would fail if 1095 // running inside chroot, because /proc may not exist.) 1096 1097 uintptr_t stack_top; 1098 address low, high; 1099 if (find_vma((address)stack_start, &low, &high)) { 1100 // success, "high" is the true stack top. (ignore "low", because initial 1101 // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.) 1102 stack_top = (uintptr_t)high; 1103 } else { 1104 // failed, likely because /proc/self/maps does not exist 1105 warning("Can't detect initial thread stack location - find_vma failed"); 1106 // best effort: stack_start is normally within a few pages below the real 1107 // stack top, use it as stack top, and reduce stack size so we won't put 1108 // guard page outside stack. 1109 stack_top = stack_start; 1110 stack_size -= 16 * page_size(); 1111 } 1112 1113 // stack_top could be partially down the page so align it 1114 stack_top = align_size_up(stack_top, page_size()); 1115 1116 // Allowed stack value is minimum of max_size and what we derived from rlimit 1117 if (max_size > 0) { 1118 _initial_thread_stack_size = MIN2(max_size, stack_size); 1119 } else { 1120 // Accept the rlimit max, but if stack is unlimited then it will be huge, so 1121 // clamp it at 8MB as we do on Solaris 1122 _initial_thread_stack_size = MIN2(stack_size, 8*M); 1123 } 1124 _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size()); 1125 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size; 1126 1127 assert(_initial_thread_stack_bottom < (address)stack_top, "overflow!"); 1128 1129 if (log_is_enabled(Info, os, thread)) { 1130 // See if we seem to be on primordial process thread 1131 bool primordial = uintptr_t(&rlim) > uintptr_t(_initial_thread_stack_bottom) && 1132 uintptr_t(&rlim) < stack_top; 1133 1134 log_info(os, thread)("Capturing initial stack in %s thread: req. size: " SIZE_FORMAT "K, actual size: " 1135 SIZE_FORMAT "K, top=" INTPTR_FORMAT ", bottom=" INTPTR_FORMAT, 1136 primordial ? "primordial" : "user", max_size / K, _initial_thread_stack_size / K, 1137 stack_top, intptr_t(_initial_thread_stack_bottom)); 1138 } 1139 } 1140 1141 //////////////////////////////////////////////////////////////////////////////// 1142 // time support 1143 1144 // Time since start-up in seconds to a fine granularity. 1145 // Used by VMSelfDestructTimer and the MemProfiler. 1146 double os::elapsedTime() { 1147 1148 return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution 1149 } 1150 1151 jlong os::elapsed_counter() { 1152 return javaTimeNanos() - initial_time_count; 1153 } 1154 1155 jlong os::elapsed_frequency() { 1156 return NANOSECS_PER_SEC; // nanosecond resolution 1157 } 1158 1159 bool os::supports_vtime() { return true; } 1160 bool os::enable_vtime() { return false; } 1161 bool os::vtime_enabled() { return false; } 1162 1163 double os::elapsedVTime() { 1164 struct rusage usage; 1165 int retval = getrusage(RUSAGE_THREAD, &usage); 1166 if (retval == 0) { 1167 return (double) (usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) + (double) (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / (1000 * 1000); 1168 } else { 1169 // better than nothing, but not much 1170 return elapsedTime(); 1171 } 1172 } 1173 1174 jlong os::javaTimeMillis() { 1175 timeval time; 1176 int status = gettimeofday(&time, NULL); 1177 assert(status != -1, "linux error"); 1178 return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000); 1179 } 1180 1181 void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) { 1182 timeval time; 1183 int status = gettimeofday(&time, NULL); 1184 assert(status != -1, "linux error"); 1185 seconds = jlong(time.tv_sec); 1186 nanos = jlong(time.tv_usec) * 1000; 1187 } 1188 1189 1190 #ifndef CLOCK_MONOTONIC 1191 #define CLOCK_MONOTONIC (1) 1192 #endif 1193 1194 void os::Linux::clock_init() { 1195 // we do dlopen's in this particular order due to bug in linux 1196 // dynamical loader (see 6348968) leading to crash on exit 1197 void* handle = dlopen("librt.so.1", RTLD_LAZY); 1198 if (handle == NULL) { 1199 handle = dlopen("librt.so", RTLD_LAZY); 1200 } 1201 1202 if (handle) { 1203 int (*clock_getres_func)(clockid_t, struct timespec*) = 1204 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres"); 1205 int (*clock_gettime_func)(clockid_t, struct timespec*) = 1206 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime"); 1207 if (clock_getres_func && clock_gettime_func) { 1208 // See if monotonic clock is supported by the kernel. Note that some 1209 // early implementations simply return kernel jiffies (updated every 1210 // 1/100 or 1/1000 second). It would be bad to use such a low res clock 1211 // for nano time (though the monotonic property is still nice to have). 1212 // It's fixed in newer kernels, however clock_getres() still returns 1213 // 1/HZ. We check if clock_getres() works, but will ignore its reported 1214 // resolution for now. Hopefully as people move to new kernels, this 1215 // won't be a problem. 1216 struct timespec res; 1217 struct timespec tp; 1218 if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 && 1219 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) { 1220 // yes, monotonic clock is supported 1221 _clock_gettime = clock_gettime_func; 1222 return; 1223 } else { 1224 // close librt if there is no monotonic clock 1225 dlclose(handle); 1226 } 1227 } 1228 } 1229 warning("No monotonic clock was available - timed services may " \ 1230 "be adversely affected if the time-of-day clock changes"); 1231 } 1232 1233 #ifndef SYS_clock_getres 1234 #if defined(X86) || defined(PPC64) || defined(S390) 1235 #define SYS_clock_getres AMD64_ONLY(229) IA32_ONLY(266) PPC64_ONLY(247) S390_ONLY(261) 1236 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y) 1237 #else 1238 #warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time" 1239 #define sys_clock_getres(x,y) -1 1240 #endif 1241 #else 1242 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y) 1243 #endif 1244 1245 void os::Linux::fast_thread_clock_init() { 1246 if (!UseLinuxPosixThreadCPUClocks) { 1247 return; 1248 } 1249 clockid_t clockid; 1250 struct timespec tp; 1251 int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) = 1252 (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid"); 1253 1254 // Switch to using fast clocks for thread cpu time if 1255 // the sys_clock_getres() returns 0 error code. 1256 // Note, that some kernels may support the current thread 1257 // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks 1258 // returned by the pthread_getcpuclockid(). 1259 // If the fast Posix clocks are supported then the sys_clock_getres() 1260 // must return at least tp.tv_sec == 0 which means a resolution 1261 // better than 1 sec. This is extra check for reliability. 1262 1263 if (pthread_getcpuclockid_func && 1264 pthread_getcpuclockid_func(_main_thread, &clockid) == 0 && 1265 sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) { 1266 _supports_fast_thread_cpu_time = true; 1267 _pthread_getcpuclockid = pthread_getcpuclockid_func; 1268 } 1269 } 1270 1271 jlong os::javaTimeNanos() { 1272 if (os::supports_monotonic_clock()) { 1273 struct timespec tp; 1274 int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp); 1275 assert(status == 0, "gettime error"); 1276 jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec); 1277 return result; 1278 } else { 1279 timeval time; 1280 int status = gettimeofday(&time, NULL); 1281 assert(status != -1, "linux error"); 1282 jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec); 1283 return 1000 * usecs; 1284 } 1285 } 1286 1287 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) { 1288 if (os::supports_monotonic_clock()) { 1289 info_ptr->max_value = ALL_64_BITS; 1290 1291 // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past 1292 info_ptr->may_skip_backward = false; // not subject to resetting or drifting 1293 info_ptr->may_skip_forward = false; // not subject to resetting or drifting 1294 } else { 1295 // gettimeofday - based on time in seconds since the Epoch thus does not wrap 1296 info_ptr->max_value = ALL_64_BITS; 1297 1298 // gettimeofday is a real time clock so it skips 1299 info_ptr->may_skip_backward = true; 1300 info_ptr->may_skip_forward = true; 1301 } 1302 1303 info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time 1304 } 1305 1306 // Return the real, user, and system times in seconds from an 1307 // arbitrary fixed point in the past. 1308 bool os::getTimesSecs(double* process_real_time, 1309 double* process_user_time, 1310 double* process_system_time) { 1311 struct tms ticks; 1312 clock_t real_ticks = times(&ticks); 1313 1314 if (real_ticks == (clock_t) (-1)) { 1315 return false; 1316 } else { 1317 double ticks_per_second = (double) clock_tics_per_sec; 1318 *process_user_time = ((double) ticks.tms_utime) / ticks_per_second; 1319 *process_system_time = ((double) ticks.tms_stime) / ticks_per_second; 1320 *process_real_time = ((double) real_ticks) / ticks_per_second; 1321 1322 return true; 1323 } 1324 } 1325 1326 1327 char * os::local_time_string(char *buf, size_t buflen) { 1328 struct tm t; 1329 time_t long_time; 1330 time(&long_time); 1331 localtime_r(&long_time, &t); 1332 jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d", 1333 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, 1334 t.tm_hour, t.tm_min, t.tm_sec); 1335 return buf; 1336 } 1337 1338 struct tm* os::localtime_pd(const time_t* clock, struct tm* res) { 1339 return localtime_r(clock, res); 1340 } 1341 1342 //////////////////////////////////////////////////////////////////////////////// 1343 // runtime exit support 1344 1345 // Note: os::shutdown() might be called very early during initialization, or 1346 // called from signal handler. Before adding something to os::shutdown(), make 1347 // sure it is async-safe and can handle partially initialized VM. 1348 void os::shutdown() { 1349 1350 // allow PerfMemory to attempt cleanup of any persistent resources 1351 perfMemory_exit(); 1352 1353 // needs to remove object in file system 1354 AttachListener::abort(); 1355 1356 // flush buffered output, finish log files 1357 ostream_abort(); 1358 1359 // Check for abort hook 1360 abort_hook_t abort_hook = Arguments::abort_hook(); 1361 if (abort_hook != NULL) { 1362 abort_hook(); 1363 } 1364 1365 } 1366 1367 // Note: os::abort() might be called very early during initialization, or 1368 // called from signal handler. Before adding something to os::abort(), make 1369 // sure it is async-safe and can handle partially initialized VM. 1370 void os::abort(bool dump_core, void* siginfo, const void* context) { 1371 os::shutdown(); 1372 if (dump_core) { 1373 #ifndef PRODUCT 1374 fdStream out(defaultStream::output_fd()); 1375 out.print_raw("Current thread is "); 1376 char buf[16]; 1377 jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id()); 1378 out.print_raw_cr(buf); 1379 out.print_raw_cr("Dumping core ..."); 1380 #endif 1381 ::abort(); // dump core 1382 } 1383 1384 ::exit(1); 1385 } 1386 1387 // Die immediately, no exit hook, no abort hook, no cleanup. 1388 void os::die() { 1389 ::abort(); 1390 } 1391 1392 1393 // This method is a copy of JDK's sysGetLastErrorString 1394 // from src/solaris/hpi/src/system_md.c 1395 1396 size_t os::lasterror(char *buf, size_t len) { 1397 if (errno == 0) return 0; 1398 1399 const char *s = os::strerror(errno); 1400 size_t n = ::strlen(s); 1401 if (n >= len) { 1402 n = len - 1; 1403 } 1404 ::strncpy(buf, s, n); 1405 buf[n] = '\0'; 1406 return n; 1407 } 1408 1409 // thread_id is kernel thread id (similar to Solaris LWP id) 1410 intx os::current_thread_id() { return os::Linux::gettid(); } 1411 int os::current_process_id() { 1412 return ::getpid(); 1413 } 1414 1415 // DLL functions 1416 1417 const char* os::dll_file_extension() { return ".so"; } 1418 1419 // This must be hard coded because it's the system's temporary 1420 // directory not the java application's temp directory, ala java.io.tmpdir. 1421 const char* os::get_temp_directory() { return "/tmp"; } 1422 1423 static bool file_exists(const char* filename) { 1424 struct stat statbuf; 1425 if (filename == NULL || strlen(filename) == 0) { 1426 return false; 1427 } 1428 return os::stat(filename, &statbuf) == 0; 1429 } 1430 1431 bool os::dll_build_name(char* buffer, size_t buflen, 1432 const char* pname, const char* fname) { 1433 bool retval = false; 1434 // Copied from libhpi 1435 const size_t pnamelen = pname ? strlen(pname) : 0; 1436 1437 // Return error on buffer overflow. 1438 if (pnamelen + strlen(fname) + 10 > (size_t) buflen) { 1439 return retval; 1440 } 1441 1442 if (pnamelen == 0) { 1443 snprintf(buffer, buflen, "lib%s.so", fname); 1444 retval = true; 1445 } else if (strchr(pname, *os::path_separator()) != NULL) { 1446 int n; 1447 char** pelements = split_path(pname, &n); 1448 if (pelements == NULL) { 1449 return false; 1450 } 1451 for (int i = 0; i < n; i++) { 1452 // Really shouldn't be NULL, but check can't hurt 1453 if (pelements[i] == NULL || strlen(pelements[i]) == 0) { 1454 continue; // skip the empty path values 1455 } 1456 snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname); 1457 if (file_exists(buffer)) { 1458 retval = true; 1459 break; 1460 } 1461 } 1462 // release the storage 1463 for (int i = 0; i < n; i++) { 1464 if (pelements[i] != NULL) { 1465 FREE_C_HEAP_ARRAY(char, pelements[i]); 1466 } 1467 } 1468 if (pelements != NULL) { 1469 FREE_C_HEAP_ARRAY(char*, pelements); 1470 } 1471 } else { 1472 snprintf(buffer, buflen, "%s/lib%s.so", pname, fname); 1473 retval = true; 1474 } 1475 return retval; 1476 } 1477 1478 // check if addr is inside libjvm.so 1479 bool os::address_is_in_vm(address addr) { 1480 static address libjvm_base_addr; 1481 Dl_info dlinfo; 1482 1483 if (libjvm_base_addr == NULL) { 1484 if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) { 1485 libjvm_base_addr = (address)dlinfo.dli_fbase; 1486 } 1487 assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm"); 1488 } 1489 1490 if (dladdr((void *)addr, &dlinfo) != 0) { 1491 if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true; 1492 } 1493 1494 return false; 1495 } 1496 1497 bool os::dll_address_to_function_name(address addr, char *buf, 1498 int buflen, int *offset, 1499 bool demangle) { 1500 // buf is not optional, but offset is optional 1501 assert(buf != NULL, "sanity check"); 1502 1503 Dl_info dlinfo; 1504 1505 if (dladdr((void*)addr, &dlinfo) != 0) { 1506 // see if we have a matching symbol 1507 if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) { 1508 if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) { 1509 jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname); 1510 } 1511 if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr; 1512 return true; 1513 } 1514 // no matching symbol so try for just file info 1515 if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) { 1516 if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase), 1517 buf, buflen, offset, dlinfo.dli_fname, demangle)) { 1518 return true; 1519 } 1520 } 1521 } 1522 1523 buf[0] = '\0'; 1524 if (offset != NULL) *offset = -1; 1525 return false; 1526 } 1527 1528 struct _address_to_library_name { 1529 address addr; // input : memory address 1530 size_t buflen; // size of fname 1531 char* fname; // output: library name 1532 address base; // library base addr 1533 }; 1534 1535 static int address_to_library_name_callback(struct dl_phdr_info *info, 1536 size_t size, void *data) { 1537 int i; 1538 bool found = false; 1539 address libbase = NULL; 1540 struct _address_to_library_name * d = (struct _address_to_library_name *)data; 1541 1542 // iterate through all loadable segments 1543 for (i = 0; i < info->dlpi_phnum; i++) { 1544 address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr); 1545 if (info->dlpi_phdr[i].p_type == PT_LOAD) { 1546 // base address of a library is the lowest address of its loaded 1547 // segments. 1548 if (libbase == NULL || libbase > segbase) { 1549 libbase = segbase; 1550 } 1551 // see if 'addr' is within current segment 1552 if (segbase <= d->addr && 1553 d->addr < segbase + info->dlpi_phdr[i].p_memsz) { 1554 found = true; 1555 } 1556 } 1557 } 1558 1559 // dlpi_name is NULL or empty if the ELF file is executable, return 0 1560 // so dll_address_to_library_name() can fall through to use dladdr() which 1561 // can figure out executable name from argv[0]. 1562 if (found && info->dlpi_name && info->dlpi_name[0]) { 1563 d->base = libbase; 1564 if (d->fname) { 1565 jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name); 1566 } 1567 return 1; 1568 } 1569 return 0; 1570 } 1571 1572 bool os::dll_address_to_library_name(address addr, char* buf, 1573 int buflen, int* offset) { 1574 // buf is not optional, but offset is optional 1575 assert(buf != NULL, "sanity check"); 1576 1577 Dl_info dlinfo; 1578 struct _address_to_library_name data; 1579 1580 // There is a bug in old glibc dladdr() implementation that it could resolve 1581 // to wrong library name if the .so file has a base address != NULL. Here 1582 // we iterate through the program headers of all loaded libraries to find 1583 // out which library 'addr' really belongs to. This workaround can be 1584 // removed once the minimum requirement for glibc is moved to 2.3.x. 1585 data.addr = addr; 1586 data.fname = buf; 1587 data.buflen = buflen; 1588 data.base = NULL; 1589 int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data); 1590 1591 if (rslt) { 1592 // buf already contains library name 1593 if (offset) *offset = addr - data.base; 1594 return true; 1595 } 1596 if (dladdr((void*)addr, &dlinfo) != 0) { 1597 if (dlinfo.dli_fname != NULL) { 1598 jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname); 1599 } 1600 if (dlinfo.dli_fbase != NULL && offset != NULL) { 1601 *offset = addr - (address)dlinfo.dli_fbase; 1602 } 1603 return true; 1604 } 1605 1606 buf[0] = '\0'; 1607 if (offset) *offset = -1; 1608 return false; 1609 } 1610 1611 // Loads .dll/.so and 1612 // in case of error it checks if .dll/.so was built for the 1613 // same architecture as Hotspot is running on 1614 1615 1616 // Remember the stack's state. The Linux dynamic linker will change 1617 // the stack to 'executable' at most once, so we must safepoint only once. 1618 bool os::Linux::_stack_is_executable = false; 1619 1620 // VM operation that loads a library. This is necessary if stack protection 1621 // of the Java stacks can be lost during loading the library. If we 1622 // do not stop the Java threads, they can stack overflow before the stacks 1623 // are protected again. 1624 class VM_LinuxDllLoad: public VM_Operation { 1625 private: 1626 const char *_filename; 1627 char *_ebuf; 1628 int _ebuflen; 1629 void *_lib; 1630 public: 1631 VM_LinuxDllLoad(const char *fn, char *ebuf, int ebuflen) : 1632 _filename(fn), _ebuf(ebuf), _ebuflen(ebuflen), _lib(NULL) {} 1633 VMOp_Type type() const { return VMOp_LinuxDllLoad; } 1634 void doit() { 1635 _lib = os::Linux::dll_load_in_vmthread(_filename, _ebuf, _ebuflen); 1636 os::Linux::_stack_is_executable = true; 1637 } 1638 void* loaded_library() { return _lib; } 1639 }; 1640 1641 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) { 1642 void * result = NULL; 1643 bool load_attempted = false; 1644 1645 // Check whether the library to load might change execution rights 1646 // of the stack. If they are changed, the protection of the stack 1647 // guard pages will be lost. We need a safepoint to fix this. 1648 // 1649 // See Linux man page execstack(8) for more info. 1650 if (os::uses_stack_guard_pages() && !os::Linux::_stack_is_executable) { 1651 ElfFile ef(filename); 1652 if (!ef.specifies_noexecstack()) { 1653 if (!is_init_completed()) { 1654 os::Linux::_stack_is_executable = true; 1655 // This is OK - No Java threads have been created yet, and hence no 1656 // stack guard pages to fix. 1657 // 1658 // This should happen only when you are building JDK7 using a very 1659 // old version of JDK6 (e.g., with JPRT) and running test_gamma. 1660 // 1661 // Dynamic loader will make all stacks executable after 1662 // this function returns, and will not do that again. 1663 assert(Threads::first() == NULL, "no Java threads should exist yet."); 1664 } else { 1665 warning("You have loaded library %s which might have disabled stack guard. " 1666 "The VM will try to fix the stack guard now.\n" 1667 "It's highly recommended that you fix the library with " 1668 "'execstack -c <libfile>', or link it with '-z noexecstack'.", 1669 filename); 1670 1671 assert(Thread::current()->is_Java_thread(), "must be Java thread"); 1672 JavaThread *jt = JavaThread::current(); 1673 if (jt->thread_state() != _thread_in_native) { 1674 // This happens when a compiler thread tries to load a hsdis-<arch>.so file 1675 // that requires ExecStack. Cannot enter safe point. Let's give up. 1676 warning("Unable to fix stack guard. Giving up."); 1677 } else { 1678 if (!LoadExecStackDllInVMThread) { 1679 // This is for the case where the DLL has an static 1680 // constructor function that executes JNI code. We cannot 1681 // load such DLLs in the VMThread. 1682 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen); 1683 } 1684 1685 ThreadInVMfromNative tiv(jt); 1686 debug_only(VMNativeEntryWrapper vew;) 1687 1688 VM_LinuxDllLoad op(filename, ebuf, ebuflen); 1689 VMThread::execute(&op); 1690 if (LoadExecStackDllInVMThread) { 1691 result = op.loaded_library(); 1692 } 1693 load_attempted = true; 1694 } 1695 } 1696 } 1697 } 1698 1699 if (!load_attempted) { 1700 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen); 1701 } 1702 1703 if (result != NULL) { 1704 // Successful loading 1705 return result; 1706 } 1707 1708 Elf32_Ehdr elf_head; 1709 int diag_msg_max_length=ebuflen-strlen(ebuf); 1710 char* diag_msg_buf=ebuf+strlen(ebuf); 1711 1712 if (diag_msg_max_length==0) { 1713 // No more space in ebuf for additional diagnostics message 1714 return NULL; 1715 } 1716 1717 1718 int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK); 1719 1720 if (file_descriptor < 0) { 1721 // Can't open library, report dlerror() message 1722 return NULL; 1723 } 1724 1725 bool failed_to_read_elf_head= 1726 (sizeof(elf_head)!= 1727 (::read(file_descriptor, &elf_head,sizeof(elf_head)))); 1728 1729 ::close(file_descriptor); 1730 if (failed_to_read_elf_head) { 1731 // file i/o error - report dlerror() msg 1732 return NULL; 1733 } 1734 1735 typedef struct { 1736 Elf32_Half code; // Actual value as defined in elf.h 1737 Elf32_Half compat_class; // Compatibility of archs at VM's sense 1738 unsigned char elf_class; // 32 or 64 bit 1739 unsigned char endianess; // MSB or LSB 1740 char* name; // String representation 1741 } arch_t; 1742 1743 #ifndef EM_486 1744 #define EM_486 6 /* Intel 80486 */ 1745 #endif 1746 #ifndef EM_AARCH64 1747 #define EM_AARCH64 183 /* ARM AARCH64 */ 1748 #endif 1749 1750 static const arch_t arch_array[]={ 1751 {EM_386, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"}, 1752 {EM_486, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"}, 1753 {EM_IA_64, EM_IA_64, ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"}, 1754 {EM_X86_64, EM_X86_64, ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"}, 1755 {EM_SPARC, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"}, 1756 {EM_SPARC32PLUS, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"}, 1757 {EM_SPARCV9, EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"}, 1758 {EM_PPC, EM_PPC, ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"}, 1759 #if defined(VM_LITTLE_ENDIAN) 1760 {EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2LSB, (char*)"Power PC 64"}, 1761 #else 1762 {EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64 LE"}, 1763 #endif 1764 {EM_ARM, EM_ARM, ELFCLASS32, ELFDATA2LSB, (char*)"ARM"}, 1765 {EM_S390, EM_S390, ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"}, 1766 {EM_ALPHA, EM_ALPHA, ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"}, 1767 {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"}, 1768 {EM_MIPS, EM_MIPS, ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"}, 1769 {EM_PARISC, EM_PARISC, ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"}, 1770 {EM_68K, EM_68K, ELFCLASS32, ELFDATA2MSB, (char*)"M68k"}, 1771 {EM_AARCH64, EM_AARCH64, ELFCLASS64, ELFDATA2LSB, (char*)"AARCH64"}, 1772 }; 1773 1774 #if (defined IA32) 1775 static Elf32_Half running_arch_code=EM_386; 1776 #elif (defined AMD64) 1777 static Elf32_Half running_arch_code=EM_X86_64; 1778 #elif (defined IA64) 1779 static Elf32_Half running_arch_code=EM_IA_64; 1780 #elif (defined __sparc) && (defined _LP64) 1781 static Elf32_Half running_arch_code=EM_SPARCV9; 1782 #elif (defined __sparc) && (!defined _LP64) 1783 static Elf32_Half running_arch_code=EM_SPARC; 1784 #elif (defined __powerpc64__) 1785 static Elf32_Half running_arch_code=EM_PPC64; 1786 #elif (defined __powerpc__) 1787 static Elf32_Half running_arch_code=EM_PPC; 1788 #elif (defined AARCH64) 1789 static Elf32_Half running_arch_code=EM_AARCH64; 1790 #elif (defined ARM) 1791 static Elf32_Half running_arch_code=EM_ARM; 1792 #elif (defined S390) 1793 static Elf32_Half running_arch_code=EM_S390; 1794 #elif (defined ALPHA) 1795 static Elf32_Half running_arch_code=EM_ALPHA; 1796 #elif (defined MIPSEL) 1797 static Elf32_Half running_arch_code=EM_MIPS_RS3_LE; 1798 #elif (defined PARISC) 1799 static Elf32_Half running_arch_code=EM_PARISC; 1800 #elif (defined MIPS) 1801 static Elf32_Half running_arch_code=EM_MIPS; 1802 #elif (defined M68K) 1803 static Elf32_Half running_arch_code=EM_68K; 1804 #else 1805 #error Method os::dll_load requires that one of following is defined:\ 1806 AARCH64, ALPHA, ARM, AMD64, IA32, IA64, M68K, MIPS, MIPSEL, PARISC, __powerpc__, __powerpc64__, S390, __sparc 1807 #endif 1808 1809 // Identify compatability class for VM's architecture and library's architecture 1810 // Obtain string descriptions for architectures 1811 1812 arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL}; 1813 int running_arch_index=-1; 1814 1815 for (unsigned int i=0; i < ARRAY_SIZE(arch_array); i++) { 1816 if (running_arch_code == arch_array[i].code) { 1817 running_arch_index = i; 1818 } 1819 if (lib_arch.code == arch_array[i].code) { 1820 lib_arch.compat_class = arch_array[i].compat_class; 1821 lib_arch.name = arch_array[i].name; 1822 } 1823 } 1824 1825 assert(running_arch_index != -1, 1826 "Didn't find running architecture code (running_arch_code) in arch_array"); 1827 if (running_arch_index == -1) { 1828 // Even though running architecture detection failed 1829 // we may still continue with reporting dlerror() message 1830 return NULL; 1831 } 1832 1833 if (lib_arch.endianess != arch_array[running_arch_index].endianess) { 1834 ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)"); 1835 return NULL; 1836 } 1837 1838 #ifndef S390 1839 if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) { 1840 ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)"); 1841 return NULL; 1842 } 1843 #endif // !S390 1844 1845 if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) { 1846 if (lib_arch.name!=NULL) { 1847 ::snprintf(diag_msg_buf, diag_msg_max_length-1, 1848 " (Possible cause: can't load %s-bit .so on a %s-bit platform)", 1849 lib_arch.name, arch_array[running_arch_index].name); 1850 } else { 1851 ::snprintf(diag_msg_buf, diag_msg_max_length-1, 1852 " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)", 1853 lib_arch.code, 1854 arch_array[running_arch_index].name); 1855 } 1856 } 1857 1858 return NULL; 1859 } 1860 1861 void * os::Linux::dlopen_helper(const char *filename, char *ebuf, 1862 int ebuflen) { 1863 void * result = ::dlopen(filename, RTLD_LAZY); 1864 if (result == NULL) { 1865 ::strncpy(ebuf, ::dlerror(), ebuflen - 1); 1866 ebuf[ebuflen-1] = '\0'; 1867 } 1868 return result; 1869 } 1870 1871 void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf, 1872 int ebuflen) { 1873 void * result = NULL; 1874 if (LoadExecStackDllInVMThread) { 1875 result = dlopen_helper(filename, ebuf, ebuflen); 1876 } 1877 1878 // Since 7019808, libjvm.so is linked with -noexecstack. If the VM loads a 1879 // library that requires an executable stack, or which does not have this 1880 // stack attribute set, dlopen changes the stack attribute to executable. The 1881 // read protection of the guard pages gets lost. 1882 // 1883 // Need to check _stack_is_executable again as multiple VM_LinuxDllLoad 1884 // may have been queued at the same time. 1885 1886 if (!_stack_is_executable) { 1887 JavaThread *jt = Threads::first(); 1888 1889 while (jt) { 1890 if (!jt->stack_guard_zone_unused() && // Stack not yet fully initialized 1891 jt->stack_guards_enabled()) { // No pending stack overflow exceptions 1892 if (!os::guard_memory((char *)jt->stack_end(), jt->stack_guard_zone_size())) { 1893 warning("Attempt to reguard stack yellow zone failed."); 1894 } 1895 } 1896 jt = jt->next(); 1897 } 1898 } 1899 1900 return result; 1901 } 1902 1903 void* os::dll_lookup(void* handle, const char* name) { 1904 void* res = dlsym(handle, name); 1905 return res; 1906 } 1907 1908 void* os::get_default_process_handle() { 1909 return (void*)::dlopen(NULL, RTLD_LAZY); 1910 } 1911 1912 static bool _print_ascii_file(const char* filename, outputStream* st) { 1913 int fd = ::open(filename, O_RDONLY); 1914 if (fd == -1) { 1915 return false; 1916 } 1917 1918 char buf[33]; 1919 int bytes; 1920 buf[32] = '\0'; 1921 while ((bytes = ::read(fd, buf, sizeof(buf)-1)) > 0) { 1922 st->print_raw(buf, bytes); 1923 } 1924 1925 ::close(fd); 1926 1927 return true; 1928 } 1929 1930 void os::print_dll_info(outputStream *st) { 1931 st->print_cr("Dynamic libraries:"); 1932 1933 char fname[32]; 1934 pid_t pid = os::Linux::gettid(); 1935 1936 jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid); 1937 1938 if (!_print_ascii_file(fname, st)) { 1939 st->print("Can not get library information for pid = %d\n", pid); 1940 } 1941 } 1942 1943 int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) { 1944 FILE *procmapsFile = NULL; 1945 1946 // Open the procfs maps file for the current process 1947 if ((procmapsFile = fopen("/proc/self/maps", "r")) != NULL) { 1948 // Allocate PATH_MAX for file name plus a reasonable size for other fields. 1949 char line[PATH_MAX + 100]; 1950 1951 // Read line by line from 'file' 1952 while (fgets(line, sizeof(line), procmapsFile) != NULL) { 1953 u8 base, top, offset, inode; 1954 char permissions[5]; 1955 char device[6]; 1956 char name[PATH_MAX + 1]; 1957 1958 // Parse fields from line 1959 sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %4s " UINT64_FORMAT_X " %5s " INT64_FORMAT " %s", 1960 &base, &top, permissions, &offset, device, &inode, name); 1961 1962 // Filter by device id '00:00' so that we only get file system mapped files. 1963 if (strcmp(device, "00:00") != 0) { 1964 1965 // Call callback with the fields of interest 1966 if(callback(name, (address)base, (address)top, param)) { 1967 // Oops abort, callback aborted 1968 fclose(procmapsFile); 1969 return 1; 1970 } 1971 } 1972 } 1973 fclose(procmapsFile); 1974 } 1975 return 0; 1976 } 1977 1978 void os::print_os_info_brief(outputStream* st) { 1979 os::Linux::print_distro_info(st); 1980 1981 os::Posix::print_uname_info(st); 1982 1983 os::Linux::print_libversion_info(st); 1984 1985 } 1986 1987 void os::print_os_info(outputStream* st) { 1988 st->print("OS:"); 1989 1990 os::Linux::print_distro_info(st); 1991 1992 os::Posix::print_uname_info(st); 1993 1994 // Print warning if unsafe chroot environment detected 1995 if (unsafe_chroot_detected) { 1996 st->print("WARNING!! "); 1997 st->print_cr("%s", unstable_chroot_error); 1998 } 1999 2000 os::Linux::print_libversion_info(st); 2001 2002 os::Posix::print_rlimit_info(st); 2003 2004 os::Posix::print_load_average(st); 2005 2006 os::Linux::print_full_memory_info(st); 2007 } 2008 2009 // Try to identify popular distros. 2010 // Most Linux distributions have a /etc/XXX-release file, which contains 2011 // the OS version string. Newer Linux distributions have a /etc/lsb-release 2012 // file that also contains the OS version string. Some have more than one 2013 // /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and 2014 // /etc/redhat-release.), so the order is important. 2015 // Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have 2016 // their own specific XXX-release file as well as a redhat-release file. 2017 // Because of this the XXX-release file needs to be searched for before the 2018 // redhat-release file. 2019 // Since Red Hat and SuSE have an lsb-release file that is not very descriptive the 2020 // search for redhat-release / SuSE-release needs to be before lsb-release. 2021 // Since the lsb-release file is the new standard it needs to be searched 2022 // before the older style release files. 2023 // Searching system-release (Red Hat) and os-release (other Linuxes) are a 2024 // next to last resort. The os-release file is a new standard that contains 2025 // distribution information and the system-release file seems to be an old 2026 // standard that has been replaced by the lsb-release and os-release files. 2027 // Searching for the debian_version file is the last resort. It contains 2028 // an informative string like "6.0.6" or "wheezy/sid". Because of this 2029 // "Debian " is printed before the contents of the debian_version file. 2030 2031 const char* distro_files[] = { 2032 "/etc/oracle-release", 2033 "/etc/mandriva-release", 2034 "/etc/mandrake-release", 2035 "/etc/sun-release", 2036 "/etc/redhat-release", 2037 "/etc/SuSE-release", 2038 "/etc/lsb-release", 2039 "/etc/turbolinux-release", 2040 "/etc/gentoo-release", 2041 "/etc/ltib-release", 2042 "/etc/angstrom-version", 2043 "/etc/system-release", 2044 "/etc/os-release", 2045 NULL }; 2046 2047 void os::Linux::print_distro_info(outputStream* st) { 2048 for (int i = 0;; i++) { 2049 const char* file = distro_files[i]; 2050 if (file == NULL) { 2051 break; // done 2052 } 2053 // If file prints, we found it. 2054 if (_print_ascii_file(file, st)) { 2055 return; 2056 } 2057 } 2058 2059 if (file_exists("/etc/debian_version")) { 2060 st->print("Debian "); 2061 _print_ascii_file("/etc/debian_version", st); 2062 } else { 2063 st->print("Linux"); 2064 } 2065 st->cr(); 2066 } 2067 2068 static void parse_os_info_helper(FILE* fp, char* distro, size_t length, bool get_first_line) { 2069 char buf[256]; 2070 while (fgets(buf, sizeof(buf), fp)) { 2071 // Edit out extra stuff in expected format 2072 if (strstr(buf, "DISTRIB_DESCRIPTION=") != NULL || strstr(buf, "PRETTY_NAME=") != NULL) { 2073 char* ptr = strstr(buf, "\""); // the name is in quotes 2074 if (ptr != NULL) { 2075 ptr++; // go beyond first quote 2076 char* nl = strchr(ptr, '\"'); 2077 if (nl != NULL) *nl = '\0'; 2078 strncpy(distro, ptr, length); 2079 } else { 2080 ptr = strstr(buf, "="); 2081 ptr++; // go beyond equals then 2082 char* nl = strchr(ptr, '\n'); 2083 if (nl != NULL) *nl = '\0'; 2084 strncpy(distro, ptr, length); 2085 } 2086 return; 2087 } else if (get_first_line) { 2088 char* nl = strchr(buf, '\n'); 2089 if (nl != NULL) *nl = '\0'; 2090 strncpy(distro, buf, length); 2091 return; 2092 } 2093 } 2094 // print last line and close 2095 char* nl = strchr(buf, '\n'); 2096 if (nl != NULL) *nl = '\0'; 2097 strncpy(distro, buf, length); 2098 } 2099 2100 static void parse_os_info(char* distro, size_t length, const char* file) { 2101 FILE* fp = fopen(file, "r"); 2102 if (fp != NULL) { 2103 // if suse format, print out first line 2104 bool get_first_line = (strcmp(file, "/etc/SuSE-release") == 0); 2105 parse_os_info_helper(fp, distro, length, get_first_line); 2106 fclose(fp); 2107 } 2108 } 2109 2110 void os::get_summary_os_info(char* buf, size_t buflen) { 2111 for (int i = 0;; i++) { 2112 const char* file = distro_files[i]; 2113 if (file == NULL) { 2114 break; // ran out of distro_files 2115 } 2116 if (file_exists(file)) { 2117 parse_os_info(buf, buflen, file); 2118 return; 2119 } 2120 } 2121 // special case for debian 2122 if (file_exists("/etc/debian_version")) { 2123 strncpy(buf, "Debian ", buflen); 2124 parse_os_info(&buf[7], buflen-7, "/etc/debian_version"); 2125 } else { 2126 strncpy(buf, "Linux", buflen); 2127 } 2128 } 2129 2130 void os::Linux::print_libversion_info(outputStream* st) { 2131 // libc, pthread 2132 st->print("libc:"); 2133 st->print("%s ", os::Linux::glibc_version()); 2134 st->print("%s ", os::Linux::libpthread_version()); 2135 st->cr(); 2136 } 2137 2138 void os::Linux::print_full_memory_info(outputStream* st) { 2139 st->print("\n/proc/meminfo:\n"); 2140 _print_ascii_file("/proc/meminfo", st); 2141 st->cr(); 2142 } 2143 2144 void os::print_memory_info(outputStream* st) { 2145 2146 st->print("Memory:"); 2147 st->print(" %dk page", os::vm_page_size()>>10); 2148 2149 // values in struct sysinfo are "unsigned long" 2150 struct sysinfo si; 2151 sysinfo(&si); 2152 2153 st->print(", physical " UINT64_FORMAT "k", 2154 os::physical_memory() >> 10); 2155 st->print("(" UINT64_FORMAT "k free)", 2156 os::available_memory() >> 10); 2157 st->print(", swap " UINT64_FORMAT "k", 2158 ((jlong)si.totalswap * si.mem_unit) >> 10); 2159 st->print("(" UINT64_FORMAT "k free)", 2160 ((jlong)si.freeswap * si.mem_unit) >> 10); 2161 st->cr(); 2162 } 2163 2164 // Print the first "model name" line and the first "flags" line 2165 // that we find and nothing more. We assume "model name" comes 2166 // before "flags" so if we find a second "model name", then the 2167 // "flags" field is considered missing. 2168 static bool print_model_name_and_flags(outputStream* st, char* buf, size_t buflen) { 2169 #if defined(IA32) || defined(AMD64) 2170 // Other platforms have less repetitive cpuinfo files 2171 FILE *fp = fopen("/proc/cpuinfo", "r"); 2172 if (fp) { 2173 while (!feof(fp)) { 2174 if (fgets(buf, buflen, fp)) { 2175 // Assume model name comes before flags 2176 bool model_name_printed = false; 2177 if (strstr(buf, "model name") != NULL) { 2178 if (!model_name_printed) { 2179 st->print_raw("CPU Model and flags from /proc/cpuinfo:\n"); 2180 st->print_raw(buf); 2181 model_name_printed = true; 2182 } else { 2183 // model name printed but not flags? Odd, just return 2184 fclose(fp); 2185 return true; 2186 } 2187 } 2188 // print the flags line too 2189 if (strstr(buf, "flags") != NULL) { 2190 st->print_raw(buf); 2191 fclose(fp); 2192 return true; 2193 } 2194 } 2195 } 2196 fclose(fp); 2197 } 2198 #endif // x86 platforms 2199 return false; 2200 } 2201 2202 void os::pd_print_cpu_info(outputStream* st, char* buf, size_t buflen) { 2203 // Only print the model name if the platform provides this as a summary 2204 if (!print_model_name_and_flags(st, buf, buflen)) { 2205 st->print("\n/proc/cpuinfo:\n"); 2206 if (!_print_ascii_file("/proc/cpuinfo", st)) { 2207 st->print_cr(" <Not Available>"); 2208 } 2209 } 2210 } 2211 2212 #if defined(AMD64) || defined(IA32) || defined(X32) 2213 const char* search_string = "model name"; 2214 #elif defined(PPC64) 2215 const char* search_string = "cpu"; 2216 #elif defined(S390) 2217 const char* search_string = "processor"; 2218 #elif defined(SPARC) 2219 const char* search_string = "cpu"; 2220 #else 2221 const char* search_string = "Processor"; 2222 #endif 2223 2224 // Parses the cpuinfo file for string representing the model name. 2225 void os::get_summary_cpu_info(char* cpuinfo, size_t length) { 2226 FILE* fp = fopen("/proc/cpuinfo", "r"); 2227 if (fp != NULL) { 2228 while (!feof(fp)) { 2229 char buf[256]; 2230 if (fgets(buf, sizeof(buf), fp)) { 2231 char* start = strstr(buf, search_string); 2232 if (start != NULL) { 2233 char *ptr = start + strlen(search_string); 2234 char *end = buf + strlen(buf); 2235 while (ptr != end) { 2236 // skip whitespace and colon for the rest of the name. 2237 if (*ptr != ' ' && *ptr != '\t' && *ptr != ':') { 2238 break; 2239 } 2240 ptr++; 2241 } 2242 if (ptr != end) { 2243 // reasonable string, get rid of newline and keep the rest 2244 char* nl = strchr(buf, '\n'); 2245 if (nl != NULL) *nl = '\0'; 2246 strncpy(cpuinfo, ptr, length); 2247 fclose(fp); 2248 return; 2249 } 2250 } 2251 } 2252 } 2253 fclose(fp); 2254 } 2255 // cpuinfo not found or parsing failed, just print generic string. The entire 2256 // /proc/cpuinfo file will be printed later in the file (or enough of it for x86) 2257 #if defined(AARCH64) 2258 strncpy(cpuinfo, "AArch64", length); 2259 #elif defined(AMD64) 2260 strncpy(cpuinfo, "x86_64", length); 2261 #elif defined(ARM) // Order wrt. AARCH64 is relevant! 2262 strncpy(cpuinfo, "ARM", length); 2263 #elif defined(IA32) 2264 strncpy(cpuinfo, "x86_32", length); 2265 #elif defined(IA64) 2266 strncpy(cpuinfo, "IA64", length); 2267 #elif defined(PPC) 2268 strncpy(cpuinfo, "PPC64", length); 2269 #elif defined(S390) 2270 strncpy(cpuinfo, "S390", length); 2271 #elif defined(SPARC) 2272 strncpy(cpuinfo, "sparcv9", length); 2273 #elif defined(ZERO_LIBARCH) 2274 strncpy(cpuinfo, ZERO_LIBARCH, length); 2275 #else 2276 strncpy(cpuinfo, "unknown", length); 2277 #endif 2278 } 2279 2280 static void print_signal_handler(outputStream* st, int sig, 2281 char* buf, size_t buflen); 2282 2283 void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) { 2284 st->print_cr("Signal Handlers:"); 2285 print_signal_handler(st, SIGSEGV, buf, buflen); 2286 print_signal_handler(st, SIGBUS , buf, buflen); 2287 print_signal_handler(st, SIGFPE , buf, buflen); 2288 print_signal_handler(st, SIGPIPE, buf, buflen); 2289 print_signal_handler(st, SIGXFSZ, buf, buflen); 2290 print_signal_handler(st, SIGILL , buf, buflen); 2291 print_signal_handler(st, SR_signum, buf, buflen); 2292 print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen); 2293 print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen); 2294 print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen); 2295 print_signal_handler(st, BREAK_SIGNAL, buf, buflen); 2296 #if defined(PPC64) 2297 print_signal_handler(st, SIGTRAP, buf, buflen); 2298 #endif 2299 } 2300 2301 static char saved_jvm_path[MAXPATHLEN] = {0}; 2302 2303 // Find the full path to the current module, libjvm.so 2304 void os::jvm_path(char *buf, jint buflen) { 2305 // Error checking. 2306 if (buflen < MAXPATHLEN) { 2307 assert(false, "must use a large-enough buffer"); 2308 buf[0] = '\0'; 2309 return; 2310 } 2311 // Lazy resolve the path to current module. 2312 if (saved_jvm_path[0] != 0) { 2313 strcpy(buf, saved_jvm_path); 2314 return; 2315 } 2316 2317 char dli_fname[MAXPATHLEN]; 2318 bool ret = dll_address_to_library_name( 2319 CAST_FROM_FN_PTR(address, os::jvm_path), 2320 dli_fname, sizeof(dli_fname), NULL); 2321 assert(ret, "cannot locate libjvm"); 2322 char *rp = NULL; 2323 if (ret && dli_fname[0] != '\0') { 2324 rp = realpath(dli_fname, buf); 2325 } 2326 if (rp == NULL) { 2327 return; 2328 } 2329 2330 if (Arguments::sun_java_launcher_is_altjvm()) { 2331 // Support for the java launcher's '-XXaltjvm=<path>' option. Typical 2332 // value for buf is "<JAVA_HOME>/jre/lib/<vmtype>/libjvm.so". 2333 // If "/jre/lib/" appears at the right place in the string, then 2334 // assume we are installed in a JDK and we're done. Otherwise, check 2335 // for a JAVA_HOME environment variable and fix up the path so it 2336 // looks like libjvm.so is installed there (append a fake suffix 2337 // hotspot/libjvm.so). 2338 const char *p = buf + strlen(buf) - 1; 2339 for (int count = 0; p > buf && count < 5; ++count) { 2340 for (--p; p > buf && *p != '/'; --p) 2341 /* empty */ ; 2342 } 2343 2344 if (strncmp(p, "/jre/lib/", 9) != 0) { 2345 // Look for JAVA_HOME in the environment. 2346 char* java_home_var = ::getenv("JAVA_HOME"); 2347 if (java_home_var != NULL && java_home_var[0] != 0) { 2348 char* jrelib_p; 2349 int len; 2350 2351 // Check the current module name "libjvm.so". 2352 p = strrchr(buf, '/'); 2353 if (p == NULL) { 2354 return; 2355 } 2356 assert(strstr(p, "/libjvm") == p, "invalid library name"); 2357 2358 rp = realpath(java_home_var, buf); 2359 if (rp == NULL) { 2360 return; 2361 } 2362 2363 // determine if this is a legacy image or modules image 2364 // modules image doesn't have "jre" subdirectory 2365 len = strlen(buf); 2366 assert(len < buflen, "Ran out of buffer room"); 2367 jrelib_p = buf + len; 2368 snprintf(jrelib_p, buflen-len, "/jre/lib"); 2369 if (0 != access(buf, F_OK)) { 2370 snprintf(jrelib_p, buflen-len, "/lib"); 2371 } 2372 2373 if (0 == access(buf, F_OK)) { 2374 // Use current module name "libjvm.so" 2375 len = strlen(buf); 2376 snprintf(buf + len, buflen-len, "/hotspot/libjvm.so"); 2377 } else { 2378 // Go back to path of .so 2379 rp = realpath(dli_fname, buf); 2380 if (rp == NULL) { 2381 return; 2382 } 2383 } 2384 } 2385 } 2386 } 2387 2388 strncpy(saved_jvm_path, buf, MAXPATHLEN); 2389 saved_jvm_path[MAXPATHLEN - 1] = '\0'; 2390 } 2391 2392 void os::print_jni_name_prefix_on(outputStream* st, int args_size) { 2393 // no prefix required, not even "_" 2394 } 2395 2396 void os::print_jni_name_suffix_on(outputStream* st, int args_size) { 2397 // no suffix required 2398 } 2399 2400 //////////////////////////////////////////////////////////////////////////////// 2401 // sun.misc.Signal support 2402 2403 static volatile jint sigint_count = 0; 2404 2405 static void UserHandler(int sig, void *siginfo, void *context) { 2406 // 4511530 - sem_post is serialized and handled by the manager thread. When 2407 // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We 2408 // don't want to flood the manager thread with sem_post requests. 2409 if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1) { 2410 return; 2411 } 2412 2413 // Ctrl-C is pressed during error reporting, likely because the error 2414 // handler fails to abort. Let VM die immediately. 2415 if (sig == SIGINT && is_error_reported()) { 2416 os::die(); 2417 } 2418 2419 os::signal_notify(sig); 2420 } 2421 2422 void* os::user_handler() { 2423 return CAST_FROM_FN_PTR(void*, UserHandler); 2424 } 2425 2426 struct timespec PosixSemaphore::create_timespec(unsigned int sec, int nsec) { 2427 struct timespec ts; 2428 // Semaphore's are always associated with CLOCK_REALTIME 2429 os::Linux::clock_gettime(CLOCK_REALTIME, &ts); 2430 // see unpackTime for discussion on overflow checking 2431 if (sec >= MAX_SECS) { 2432 ts.tv_sec += MAX_SECS; 2433 ts.tv_nsec = 0; 2434 } else { 2435 ts.tv_sec += sec; 2436 ts.tv_nsec += nsec; 2437 if (ts.tv_nsec >= NANOSECS_PER_SEC) { 2438 ts.tv_nsec -= NANOSECS_PER_SEC; 2439 ++ts.tv_sec; // note: this must be <= max_secs 2440 } 2441 } 2442 2443 return ts; 2444 } 2445 2446 extern "C" { 2447 typedef void (*sa_handler_t)(int); 2448 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); 2449 } 2450 2451 void* os::signal(int signal_number, void* handler) { 2452 struct sigaction sigAct, oldSigAct; 2453 2454 sigfillset(&(sigAct.sa_mask)); 2455 sigAct.sa_flags = SA_RESTART|SA_SIGINFO; 2456 sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler); 2457 2458 if (sigaction(signal_number, &sigAct, &oldSigAct)) { 2459 // -1 means registration failed 2460 return (void *)-1; 2461 } 2462 2463 return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler); 2464 } 2465 2466 void os::signal_raise(int signal_number) { 2467 ::raise(signal_number); 2468 } 2469 2470 // The following code is moved from os.cpp for making this 2471 // code platform specific, which it is by its very nature. 2472 2473 // Will be modified when max signal is changed to be dynamic 2474 int os::sigexitnum_pd() { 2475 return NSIG; 2476 } 2477 2478 // a counter for each possible signal value 2479 static volatile jint pending_signals[NSIG+1] = { 0 }; 2480 2481 // Linux(POSIX) specific hand shaking semaphore. 2482 static sem_t sig_sem; 2483 static PosixSemaphore sr_semaphore; 2484 2485 void os::signal_init_pd() { 2486 // Initialize signal structures 2487 ::memset((void*)pending_signals, 0, sizeof(pending_signals)); 2488 2489 // Initialize signal semaphore 2490 ::sem_init(&sig_sem, 0, 0); 2491 } 2492 2493 void os::signal_notify(int sig) { 2494 Atomic::inc(&pending_signals[sig]); 2495 ::sem_post(&sig_sem); 2496 } 2497 2498 static int check_pending_signals(bool wait) { 2499 Atomic::store(0, &sigint_count); 2500 for (;;) { 2501 for (int i = 0; i < NSIG + 1; i++) { 2502 jint n = pending_signals[i]; 2503 if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) { 2504 return i; 2505 } 2506 } 2507 if (!wait) { 2508 return -1; 2509 } 2510 JavaThread *thread = JavaThread::current(); 2511 ThreadBlockInVM tbivm(thread); 2512 2513 bool threadIsSuspended; 2514 do { 2515 thread->set_suspend_equivalent(); 2516 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 2517 ::sem_wait(&sig_sem); 2518 2519 // were we externally suspended while we were waiting? 2520 threadIsSuspended = thread->handle_special_suspend_equivalent_condition(); 2521 if (threadIsSuspended) { 2522 // The semaphore has been incremented, but while we were waiting 2523 // another thread suspended us. We don't want to continue running 2524 // while suspended because that would surprise the thread that 2525 // suspended us. 2526 ::sem_post(&sig_sem); 2527 2528 thread->java_suspend_self(); 2529 } 2530 } while (threadIsSuspended); 2531 } 2532 } 2533 2534 int os::signal_lookup() { 2535 return check_pending_signals(false); 2536 } 2537 2538 int os::signal_wait() { 2539 return check_pending_signals(true); 2540 } 2541 2542 //////////////////////////////////////////////////////////////////////////////// 2543 // Virtual Memory 2544 2545 int os::vm_page_size() { 2546 // Seems redundant as all get out 2547 assert(os::Linux::page_size() != -1, "must call os::init"); 2548 return os::Linux::page_size(); 2549 } 2550 2551 // Solaris allocates memory by pages. 2552 int os::vm_allocation_granularity() { 2553 assert(os::Linux::page_size() != -1, "must call os::init"); 2554 return os::Linux::page_size(); 2555 } 2556 2557 // Rationale behind this function: 2558 // current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable 2559 // mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get 2560 // samples for JITted code. Here we create private executable mapping over the code cache 2561 // and then we can use standard (well, almost, as mapping can change) way to provide 2562 // info for the reporting script by storing timestamp and location of symbol 2563 void linux_wrap_code(char* base, size_t size) { 2564 static volatile jint cnt = 0; 2565 2566 if (!UseOprofile) { 2567 return; 2568 } 2569 2570 char buf[PATH_MAX+1]; 2571 int num = Atomic::add(1, &cnt); 2572 2573 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d", 2574 os::get_temp_directory(), os::current_process_id(), num); 2575 unlink(buf); 2576 2577 int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU); 2578 2579 if (fd != -1) { 2580 off_t rv = ::lseek(fd, size-2, SEEK_SET); 2581 if (rv != (off_t)-1) { 2582 if (::write(fd, "", 1) == 1) { 2583 mmap(base, size, 2584 PROT_READ|PROT_WRITE|PROT_EXEC, 2585 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0); 2586 } 2587 } 2588 ::close(fd); 2589 unlink(buf); 2590 } 2591 } 2592 2593 static bool recoverable_mmap_error(int err) { 2594 // See if the error is one we can let the caller handle. This 2595 // list of errno values comes from JBS-6843484. I can't find a 2596 // Linux man page that documents this specific set of errno 2597 // values so while this list currently matches Solaris, it may 2598 // change as we gain experience with this failure mode. 2599 switch (err) { 2600 case EBADF: 2601 case EINVAL: 2602 case ENOTSUP: 2603 // let the caller deal with these errors 2604 return true; 2605 2606 default: 2607 // Any remaining errors on this OS can cause our reserved mapping 2608 // to be lost. That can cause confusion where different data 2609 // structures think they have the same memory mapped. The worst 2610 // scenario is if both the VM and a library think they have the 2611 // same memory mapped. 2612 return false; 2613 } 2614 } 2615 2616 static void warn_fail_commit_memory(char* addr, size_t size, bool exec, 2617 int err) { 2618 warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT 2619 ", %d) failed; error='%s' (errno=%d)", p2i(addr), size, exec, 2620 os::strerror(err), err); 2621 } 2622 2623 static void warn_fail_commit_memory(char* addr, size_t size, 2624 size_t alignment_hint, bool exec, 2625 int err) { 2626 warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT 2627 ", " SIZE_FORMAT ", %d) failed; error='%s' (errno=%d)", p2i(addr), size, 2628 alignment_hint, exec, os::strerror(err), err); 2629 } 2630 2631 // NOTE: Linux kernel does not really reserve the pages for us. 2632 // All it does is to check if there are enough free pages 2633 // left at the time of mmap(). This could be a potential 2634 // problem. 2635 int os::Linux::commit_memory_impl(char* addr, size_t size, bool exec) { 2636 int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE; 2637 uintptr_t res = (uintptr_t) ::mmap(addr, size, prot, 2638 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0); 2639 if (res != (uintptr_t) MAP_FAILED) { 2640 if (UseNUMAInterleaving) { 2641 numa_make_global(addr, size); 2642 } 2643 return 0; 2644 } 2645 2646 int err = errno; // save errno from mmap() call above 2647 2648 if (!recoverable_mmap_error(err)) { 2649 warn_fail_commit_memory(addr, size, exec, err); 2650 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory."); 2651 } 2652 2653 return err; 2654 } 2655 2656 bool os::pd_commit_memory(char* addr, size_t size, bool exec) { 2657 return os::Linux::commit_memory_impl(addr, size, exec) == 0; 2658 } 2659 2660 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec, 2661 const char* mesg) { 2662 assert(mesg != NULL, "mesg must be specified"); 2663 int err = os::Linux::commit_memory_impl(addr, size, exec); 2664 if (err != 0) { 2665 // the caller wants all commit errors to exit with the specified mesg: 2666 warn_fail_commit_memory(addr, size, exec, err); 2667 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg); 2668 } 2669 } 2670 2671 // Define MAP_HUGETLB here so we can build HotSpot on old systems. 2672 #ifndef MAP_HUGETLB 2673 #define MAP_HUGETLB 0x40000 2674 #endif 2675 2676 // Define MADV_HUGEPAGE here so we can build HotSpot on old systems. 2677 #ifndef MADV_HUGEPAGE 2678 #define MADV_HUGEPAGE 14 2679 #endif 2680 2681 int os::Linux::commit_memory_impl(char* addr, size_t size, 2682 size_t alignment_hint, bool exec) { 2683 int err = os::Linux::commit_memory_impl(addr, size, exec); 2684 if (err == 0) { 2685 realign_memory(addr, size, alignment_hint); 2686 } 2687 return err; 2688 } 2689 2690 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint, 2691 bool exec) { 2692 return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0; 2693 } 2694 2695 void os::pd_commit_memory_or_exit(char* addr, size_t size, 2696 size_t alignment_hint, bool exec, 2697 const char* mesg) { 2698 assert(mesg != NULL, "mesg must be specified"); 2699 int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec); 2700 if (err != 0) { 2701 // the caller wants all commit errors to exit with the specified mesg: 2702 warn_fail_commit_memory(addr, size, alignment_hint, exec, err); 2703 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg); 2704 } 2705 } 2706 2707 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) { 2708 if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) { 2709 // We don't check the return value: madvise(MADV_HUGEPAGE) may not 2710 // be supported or the memory may already be backed by huge pages. 2711 ::madvise(addr, bytes, MADV_HUGEPAGE); 2712 } 2713 } 2714 2715 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) { 2716 // This method works by doing an mmap over an existing mmaping and effectively discarding 2717 // the existing pages. However it won't work for SHM-based large pages that cannot be 2718 // uncommitted at all. We don't do anything in this case to avoid creating a segment with 2719 // small pages on top of the SHM segment. This method always works for small pages, so we 2720 // allow that in any case. 2721 if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) { 2722 commit_memory(addr, bytes, alignment_hint, !ExecMem); 2723 } 2724 } 2725 2726 void os::numa_make_global(char *addr, size_t bytes) { 2727 Linux::numa_interleave_memory(addr, bytes); 2728 } 2729 2730 // Define for numa_set_bind_policy(int). Setting the argument to 0 will set the 2731 // bind policy to MPOL_PREFERRED for the current thread. 2732 #define USE_MPOL_PREFERRED 0 2733 2734 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) { 2735 // To make NUMA and large pages more robust when both enabled, we need to ease 2736 // the requirements on where the memory should be allocated. MPOL_BIND is the 2737 // default policy and it will force memory to be allocated on the specified 2738 // node. Changing this to MPOL_PREFERRED will prefer to allocate the memory on 2739 // the specified node, but will not force it. Using this policy will prevent 2740 // getting SIGBUS when trying to allocate large pages on NUMA nodes with no 2741 // free large pages. 2742 Linux::numa_set_bind_policy(USE_MPOL_PREFERRED); 2743 Linux::numa_tonode_memory(addr, bytes, lgrp_hint); 2744 } 2745 2746 bool os::numa_topology_changed() { return false; } 2747 2748 size_t os::numa_get_groups_num() { 2749 int max_node = Linux::numa_max_node(); 2750 return max_node > 0 ? max_node + 1 : 1; 2751 } 2752 2753 int os::numa_get_group_id() { 2754 int cpu_id = Linux::sched_getcpu(); 2755 if (cpu_id != -1) { 2756 int lgrp_id = Linux::get_node_by_cpu(cpu_id); 2757 if (lgrp_id != -1) { 2758 return lgrp_id; 2759 } 2760 } 2761 return 0; 2762 } 2763 2764 size_t os::numa_get_leaf_groups(int *ids, size_t size) { 2765 for (size_t i = 0; i < size; i++) { 2766 ids[i] = i; 2767 } 2768 return size; 2769 } 2770 2771 bool os::get_page_info(char *start, page_info* info) { 2772 return false; 2773 } 2774 2775 char *os::scan_pages(char *start, char* end, page_info* page_expected, 2776 page_info* page_found) { 2777 return end; 2778 } 2779 2780 2781 int os::Linux::sched_getcpu_syscall(void) { 2782 unsigned int cpu = 0; 2783 int retval = -1; 2784 2785 #if defined(IA32) 2786 #ifndef SYS_getcpu 2787 #define SYS_getcpu 318 2788 #endif 2789 retval = syscall(SYS_getcpu, &cpu, NULL, NULL); 2790 #elif defined(AMD64) 2791 // Unfortunately we have to bring all these macros here from vsyscall.h 2792 // to be able to compile on old linuxes. 2793 #define __NR_vgetcpu 2 2794 #define VSYSCALL_START (-10UL << 20) 2795 #define VSYSCALL_SIZE 1024 2796 #define VSYSCALL_ADDR(vsyscall_nr) (VSYSCALL_START+VSYSCALL_SIZE*(vsyscall_nr)) 2797 typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache); 2798 vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu); 2799 retval = vgetcpu(&cpu, NULL, NULL); 2800 #endif 2801 2802 return (retval == -1) ? retval : cpu; 2803 } 2804 2805 // Something to do with the numa-aware allocator needs these symbols 2806 extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { } 2807 extern "C" JNIEXPORT void numa_error(char *where) { } 2808 2809 2810 // If we are running with libnuma version > 2, then we should 2811 // be trying to use symbols with versions 1.1 2812 // If we are running with earlier version, which did not have symbol versions, 2813 // we should use the base version. 2814 void* os::Linux::libnuma_dlsym(void* handle, const char *name) { 2815 typedef void* (*dlvsym_func_type)(void* handle, const char* name, const char* version); 2816 static dlvsym_func_type dlvsym_func; 2817 static bool initialized = false; 2818 2819 if (!initialized) { 2820 dlvsym_func = (dlvsym_func_type)dlsym(RTLD_NEXT, "dlvsym"); 2821 initialized = true; 2822 } 2823 2824 if (dlvsym_func != NULL) { 2825 void *f = dlvsym_func(handle, name, "libnuma_1.1"); 2826 if (f != NULL) { 2827 return f; 2828 } 2829 } 2830 2831 return dlsym(handle, name); 2832 } 2833 2834 bool os::Linux::libnuma_init() { 2835 // sched_getcpu() should be in libc. 2836 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, 2837 dlsym(RTLD_DEFAULT, "sched_getcpu"))); 2838 2839 // If it's not, try a direct syscall. 2840 if (sched_getcpu() == -1) { 2841 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, 2842 (void*)&sched_getcpu_syscall)); 2843 } 2844 2845 if (sched_getcpu() != -1) { // Does it work? 2846 void *handle = dlopen("libnuma.so.1", RTLD_LAZY); 2847 if (handle != NULL) { 2848 set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t, 2849 libnuma_dlsym(handle, "numa_node_to_cpus"))); 2850 set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t, 2851 libnuma_dlsym(handle, "numa_max_node"))); 2852 set_numa_available(CAST_TO_FN_PTR(numa_available_func_t, 2853 libnuma_dlsym(handle, "numa_available"))); 2854 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t, 2855 libnuma_dlsym(handle, "numa_tonode_memory"))); 2856 set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t, 2857 libnuma_dlsym(handle, "numa_interleave_memory"))); 2858 set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t, 2859 libnuma_dlsym(handle, "numa_set_bind_policy"))); 2860 2861 2862 if (numa_available() != -1) { 2863 set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes")); 2864 // Create a cpu -> node mapping 2865 _cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true); 2866 rebuild_cpu_to_node_map(); 2867 return true; 2868 } 2869 } 2870 } 2871 return false; 2872 } 2873 2874 size_t os::Linux::default_guard_size(os::ThreadType thr_type) { 2875 // Creating guard page is very expensive. Java thread has HotSpot 2876 // guard pages, only enable glibc guard page for non-Java threads. 2877 // (Remember: compiler thread is a Java thread, too!) 2878 return ((thr_type == java_thread || thr_type == compiler_thread) ? 0 : page_size()); 2879 } 2880 2881 // rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id. 2882 // The table is later used in get_node_by_cpu(). 2883 void os::Linux::rebuild_cpu_to_node_map() { 2884 const size_t NCPUS = 32768; // Since the buffer size computation is very obscure 2885 // in libnuma (possible values are starting from 16, 2886 // and continuing up with every other power of 2, but less 2887 // than the maximum number of CPUs supported by kernel), and 2888 // is a subject to change (in libnuma version 2 the requirements 2889 // are more reasonable) we'll just hardcode the number they use 2890 // in the library. 2891 const size_t BitsPerCLong = sizeof(long) * CHAR_BIT; 2892 2893 size_t cpu_num = processor_count(); 2894 size_t cpu_map_size = NCPUS / BitsPerCLong; 2895 size_t cpu_map_valid_size = 2896 MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size); 2897 2898 cpu_to_node()->clear(); 2899 cpu_to_node()->at_grow(cpu_num - 1); 2900 size_t node_num = numa_get_groups_num(); 2901 2902 unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size, mtInternal); 2903 for (size_t i = 0; i < node_num; i++) { 2904 if (numa_node_to_cpus(i, cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) { 2905 for (size_t j = 0; j < cpu_map_valid_size; j++) { 2906 if (cpu_map[j] != 0) { 2907 for (size_t k = 0; k < BitsPerCLong; k++) { 2908 if (cpu_map[j] & (1UL << k)) { 2909 cpu_to_node()->at_put(j * BitsPerCLong + k, i); 2910 } 2911 } 2912 } 2913 } 2914 } 2915 } 2916 FREE_C_HEAP_ARRAY(unsigned long, cpu_map); 2917 } 2918 2919 int os::Linux::get_node_by_cpu(int cpu_id) { 2920 if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) { 2921 return cpu_to_node()->at(cpu_id); 2922 } 2923 return -1; 2924 } 2925 2926 GrowableArray<int>* os::Linux::_cpu_to_node; 2927 os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu; 2928 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus; 2929 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node; 2930 os::Linux::numa_available_func_t os::Linux::_numa_available; 2931 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory; 2932 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory; 2933 os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy; 2934 unsigned long* os::Linux::_numa_all_nodes; 2935 2936 bool os::pd_uncommit_memory(char* addr, size_t size) { 2937 uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE, 2938 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0); 2939 return res != (uintptr_t) MAP_FAILED; 2940 } 2941 2942 static address get_stack_commited_bottom(address bottom, size_t size) { 2943 address nbot = bottom; 2944 address ntop = bottom + size; 2945 2946 size_t page_sz = os::vm_page_size(); 2947 unsigned pages = size / page_sz; 2948 2949 unsigned char vec[1]; 2950 unsigned imin = 1, imax = pages + 1, imid; 2951 int mincore_return_value = 0; 2952 2953 assert(imin <= imax, "Unexpected page size"); 2954 2955 while (imin < imax) { 2956 imid = (imax + imin) / 2; 2957 nbot = ntop - (imid * page_sz); 2958 2959 // Use a trick with mincore to check whether the page is mapped or not. 2960 // mincore sets vec to 1 if page resides in memory and to 0 if page 2961 // is swapped output but if page we are asking for is unmapped 2962 // it returns -1,ENOMEM 2963 mincore_return_value = mincore(nbot, page_sz, vec); 2964 2965 if (mincore_return_value == -1) { 2966 // Page is not mapped go up 2967 // to find first mapped page 2968 if (errno != EAGAIN) { 2969 assert(errno == ENOMEM, "Unexpected mincore errno"); 2970 imax = imid; 2971 } 2972 } else { 2973 // Page is mapped go down 2974 // to find first not mapped page 2975 imin = imid + 1; 2976 } 2977 } 2978 2979 nbot = nbot + page_sz; 2980 2981 // Adjust stack bottom one page up if last checked page is not mapped 2982 if (mincore_return_value == -1) { 2983 nbot = nbot + page_sz; 2984 } 2985 2986 return nbot; 2987 } 2988 2989 2990 // Linux uses a growable mapping for the stack, and if the mapping for 2991 // the stack guard pages is not removed when we detach a thread the 2992 // stack cannot grow beyond the pages where the stack guard was 2993 // mapped. If at some point later in the process the stack expands to 2994 // that point, the Linux kernel cannot expand the stack any further 2995 // because the guard pages are in the way, and a segfault occurs. 2996 // 2997 // However, it's essential not to split the stack region by unmapping 2998 // a region (leaving a hole) that's already part of the stack mapping, 2999 // so if the stack mapping has already grown beyond the guard pages at 3000 // the time we create them, we have to truncate the stack mapping. 3001 // So, we need to know the extent of the stack mapping when 3002 // create_stack_guard_pages() is called. 3003 3004 // We only need this for stacks that are growable: at the time of 3005 // writing thread stacks don't use growable mappings (i.e. those 3006 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this 3007 // only applies to the main thread. 3008 3009 // If the (growable) stack mapping already extends beyond the point 3010 // where we're going to put our guard pages, truncate the mapping at 3011 // that point by munmap()ping it. This ensures that when we later 3012 // munmap() the guard pages we don't leave a hole in the stack 3013 // mapping. This only affects the main/initial thread 3014 3015 bool os::pd_create_stack_guard_pages(char* addr, size_t size) { 3016 if (os::Linux::is_initial_thread()) { 3017 // As we manually grow stack up to bottom inside create_attached_thread(), 3018 // it's likely that os::Linux::initial_thread_stack_bottom is mapped and 3019 // we don't need to do anything special. 3020 // Check it first, before calling heavy function. 3021 uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom(); 3022 unsigned char vec[1]; 3023 3024 if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) { 3025 // Fallback to slow path on all errors, including EAGAIN 3026 stack_extent = (uintptr_t) get_stack_commited_bottom( 3027 os::Linux::initial_thread_stack_bottom(), 3028 (size_t)addr - stack_extent); 3029 } 3030 3031 if (stack_extent < (uintptr_t)addr) { 3032 ::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent)); 3033 } 3034 } 3035 3036 return os::commit_memory(addr, size, !ExecMem); 3037 } 3038 3039 // If this is a growable mapping, remove the guard pages entirely by 3040 // munmap()ping them. If not, just call uncommit_memory(). This only 3041 // affects the main/initial thread, but guard against future OS changes 3042 // It's safe to always unmap guard pages for initial thread because we 3043 // always place it right after end of the mapped region 3044 3045 bool os::remove_stack_guard_pages(char* addr, size_t size) { 3046 uintptr_t stack_extent, stack_base; 3047 3048 if (os::Linux::is_initial_thread()) { 3049 return ::munmap(addr, size) == 0; 3050 } 3051 3052 return os::uncommit_memory(addr, size); 3053 } 3054 3055 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory 3056 // at 'requested_addr'. If there are existing memory mappings at the same 3057 // location, however, they will be overwritten. If 'fixed' is false, 3058 // 'requested_addr' is only treated as a hint, the return value may or 3059 // may not start from the requested address. Unlike Linux mmap(), this 3060 // function returns NULL to indicate failure. 3061 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) { 3062 char * addr; 3063 int flags; 3064 3065 flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS; 3066 if (fixed) { 3067 assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address"); 3068 flags |= MAP_FIXED; 3069 } 3070 3071 // Map reserved/uncommitted pages PROT_NONE so we fail early if we 3072 // touch an uncommitted page. Otherwise, the read/write might 3073 // succeed if we have enough swap space to back the physical page. 3074 addr = (char*)::mmap(requested_addr, bytes, PROT_NONE, 3075 flags, -1, 0); 3076 3077 return addr == MAP_FAILED ? NULL : addr; 3078 } 3079 3080 // Allocate (using mmap, NO_RESERVE, with small pages) at either a given request address 3081 // (req_addr != NULL) or with a given alignment. 3082 // - bytes shall be a multiple of alignment. 3083 // - req_addr can be NULL. If not NULL, it must be a multiple of alignment. 3084 // - alignment sets the alignment at which memory shall be allocated. 3085 // It must be a multiple of allocation granularity. 3086 // Returns address of memory or NULL. If req_addr was not NULL, will only return 3087 // req_addr or NULL. 3088 static char* anon_mmap_aligned(size_t bytes, size_t alignment, char* req_addr) { 3089 3090 size_t extra_size = bytes; 3091 if (req_addr == NULL && alignment > 0) { 3092 extra_size += alignment; 3093 } 3094 3095 char* start = (char*) ::mmap(req_addr, extra_size, PROT_NONE, 3096 MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, 3097 -1, 0); 3098 if (start == MAP_FAILED) { 3099 start = NULL; 3100 } else { 3101 if (req_addr != NULL) { 3102 if (start != req_addr) { 3103 ::munmap(start, extra_size); 3104 start = NULL; 3105 } 3106 } else { 3107 char* const start_aligned = (char*) align_ptr_up(start, alignment); 3108 char* const end_aligned = start_aligned + bytes; 3109 char* const end = start + extra_size; 3110 if (start_aligned > start) { 3111 ::munmap(start, start_aligned - start); 3112 } 3113 if (end_aligned < end) { 3114 ::munmap(end_aligned, end - end_aligned); 3115 } 3116 start = start_aligned; 3117 } 3118 } 3119 return start; 3120 } 3121 3122 static int anon_munmap(char * addr, size_t size) { 3123 return ::munmap(addr, size) == 0; 3124 } 3125 3126 char* os::pd_reserve_memory(size_t bytes, char* requested_addr, 3127 size_t alignment_hint) { 3128 return anon_mmap(requested_addr, bytes, (requested_addr != NULL)); 3129 } 3130 3131 bool os::pd_release_memory(char* addr, size_t size) { 3132 return anon_munmap(addr, size); 3133 } 3134 3135 static bool linux_mprotect(char* addr, size_t size, int prot) { 3136 // Linux wants the mprotect address argument to be page aligned. 3137 char* bottom = (char*)align_size_down((intptr_t)addr, os::Linux::page_size()); 3138 3139 // According to SUSv3, mprotect() should only be used with mappings 3140 // established by mmap(), and mmap() always maps whole pages. Unaligned 3141 // 'addr' likely indicates problem in the VM (e.g. trying to change 3142 // protection of malloc'ed or statically allocated memory). Check the 3143 // caller if you hit this assert. 3144 assert(addr == bottom, "sanity check"); 3145 3146 size = align_size_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size()); 3147 return ::mprotect(bottom, size, prot) == 0; 3148 } 3149 3150 // Set protections specified 3151 bool os::protect_memory(char* addr, size_t bytes, ProtType prot, 3152 bool is_committed) { 3153 unsigned int p = 0; 3154 switch (prot) { 3155 case MEM_PROT_NONE: p = PROT_NONE; break; 3156 case MEM_PROT_READ: p = PROT_READ; break; 3157 case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break; 3158 case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break; 3159 default: 3160 ShouldNotReachHere(); 3161 } 3162 // is_committed is unused. 3163 return linux_mprotect(addr, bytes, p); 3164 } 3165 3166 bool os::guard_memory(char* addr, size_t size) { 3167 return linux_mprotect(addr, size, PROT_NONE); 3168 } 3169 3170 bool os::unguard_memory(char* addr, size_t size) { 3171 return linux_mprotect(addr, size, PROT_READ|PROT_WRITE); 3172 } 3173 3174 bool os::Linux::transparent_huge_pages_sanity_check(bool warn, 3175 size_t page_size) { 3176 bool result = false; 3177 void *p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE, 3178 MAP_ANONYMOUS|MAP_PRIVATE, 3179 -1, 0); 3180 if (p != MAP_FAILED) { 3181 void *aligned_p = align_ptr_up(p, page_size); 3182 3183 result = madvise(aligned_p, page_size, MADV_HUGEPAGE) == 0; 3184 3185 munmap(p, page_size * 2); 3186 } 3187 3188 if (warn && !result) { 3189 warning("TransparentHugePages is not supported by the operating system."); 3190 } 3191 3192 return result; 3193 } 3194 3195 bool os::Linux::hugetlbfs_sanity_check(bool warn, size_t page_size) { 3196 bool result = false; 3197 void *p = mmap(NULL, page_size, PROT_READ|PROT_WRITE, 3198 MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB, 3199 -1, 0); 3200 3201 if (p != MAP_FAILED) { 3202 // We don't know if this really is a huge page or not. 3203 FILE *fp = fopen("/proc/self/maps", "r"); 3204 if (fp) { 3205 while (!feof(fp)) { 3206 char chars[257]; 3207 long x = 0; 3208 if (fgets(chars, sizeof(chars), fp)) { 3209 if (sscanf(chars, "%lx-%*x", &x) == 1 3210 && x == (long)p) { 3211 if (strstr (chars, "hugepage")) { 3212 result = true; 3213 break; 3214 } 3215 } 3216 } 3217 } 3218 fclose(fp); 3219 } 3220 munmap(p, page_size); 3221 } 3222 3223 if (warn && !result) { 3224 warning("HugeTLBFS is not supported by the operating system."); 3225 } 3226 3227 return result; 3228 } 3229 3230 // Set the coredump_filter bits to include largepages in core dump (bit 6) 3231 // 3232 // From the coredump_filter documentation: 3233 // 3234 // - (bit 0) anonymous private memory 3235 // - (bit 1) anonymous shared memory 3236 // - (bit 2) file-backed private memory 3237 // - (bit 3) file-backed shared memory 3238 // - (bit 4) ELF header pages in file-backed private memory areas (it is 3239 // effective only if the bit 2 is cleared) 3240 // - (bit 5) hugetlb private memory 3241 // - (bit 6) hugetlb shared memory 3242 // 3243 static void set_coredump_filter(void) { 3244 FILE *f; 3245 long cdm; 3246 3247 if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL) { 3248 return; 3249 } 3250 3251 if (fscanf(f, "%lx", &cdm) != 1) { 3252 fclose(f); 3253 return; 3254 } 3255 3256 rewind(f); 3257 3258 if ((cdm & LARGEPAGES_BIT) == 0) { 3259 cdm |= LARGEPAGES_BIT; 3260 fprintf(f, "%#lx", cdm); 3261 } 3262 3263 fclose(f); 3264 } 3265 3266 // Large page support 3267 3268 static size_t _large_page_size = 0; 3269 3270 size_t os::Linux::find_large_page_size() { 3271 size_t large_page_size = 0; 3272 3273 // large_page_size on Linux is used to round up heap size. x86 uses either 3274 // 2M or 4M page, depending on whether PAE (Physical Address Extensions) 3275 // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use 3276 // page as large as 256M. 3277 // 3278 // Here we try to figure out page size by parsing /proc/meminfo and looking 3279 // for a line with the following format: 3280 // Hugepagesize: 2048 kB 3281 // 3282 // If we can't determine the value (e.g. /proc is not mounted, or the text 3283 // format has been changed), we'll use the largest page size supported by 3284 // the processor. 3285 3286 #ifndef ZERO 3287 large_page_size = 3288 AARCH64_ONLY(2 * M) 3289 AMD64_ONLY(2 * M) 3290 ARM32_ONLY(2 * M) 3291 IA32_ONLY(4 * M) 3292 IA64_ONLY(256 * M) 3293 PPC_ONLY(4 * M) 3294 S390_ONLY(1 * M) 3295 SPARC_ONLY(4 * M); 3296 #endif // ZERO 3297 3298 FILE *fp = fopen("/proc/meminfo", "r"); 3299 if (fp) { 3300 while (!feof(fp)) { 3301 int x = 0; 3302 char buf[16]; 3303 if (fscanf(fp, "Hugepagesize: %d", &x) == 1) { 3304 if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) { 3305 large_page_size = x * K; 3306 break; 3307 } 3308 } else { 3309 // skip to next line 3310 for (;;) { 3311 int ch = fgetc(fp); 3312 if (ch == EOF || ch == (int)'\n') break; 3313 } 3314 } 3315 } 3316 fclose(fp); 3317 } 3318 3319 if (!FLAG_IS_DEFAULT(LargePageSizeInBytes) && LargePageSizeInBytes != large_page_size) { 3320 warning("Setting LargePageSizeInBytes has no effect on this OS. Large page size is " 3321 SIZE_FORMAT "%s.", byte_size_in_proper_unit(large_page_size), 3322 proper_unit_for_byte_size(large_page_size)); 3323 } 3324 3325 return large_page_size; 3326 } 3327 3328 size_t os::Linux::setup_large_page_size() { 3329 _large_page_size = Linux::find_large_page_size(); 3330 const size_t default_page_size = (size_t)Linux::page_size(); 3331 if (_large_page_size > default_page_size) { 3332 _page_sizes[0] = _large_page_size; 3333 _page_sizes[1] = default_page_size; 3334 _page_sizes[2] = 0; 3335 } 3336 3337 return _large_page_size; 3338 } 3339 3340 bool os::Linux::setup_large_page_type(size_t page_size) { 3341 if (FLAG_IS_DEFAULT(UseHugeTLBFS) && 3342 FLAG_IS_DEFAULT(UseSHM) && 3343 FLAG_IS_DEFAULT(UseTransparentHugePages)) { 3344 3345 // The type of large pages has not been specified by the user. 3346 3347 // Try UseHugeTLBFS and then UseSHM. 3348 UseHugeTLBFS = UseSHM = true; 3349 3350 // Don't try UseTransparentHugePages since there are known 3351 // performance issues with it turned on. This might change in the future. 3352 UseTransparentHugePages = false; 3353 } 3354 3355 if (UseTransparentHugePages) { 3356 bool warn_on_failure = !FLAG_IS_DEFAULT(UseTransparentHugePages); 3357 if (transparent_huge_pages_sanity_check(warn_on_failure, page_size)) { 3358 UseHugeTLBFS = false; 3359 UseSHM = false; 3360 return true; 3361 } 3362 UseTransparentHugePages = false; 3363 } 3364 3365 if (UseHugeTLBFS) { 3366 bool warn_on_failure = !FLAG_IS_DEFAULT(UseHugeTLBFS); 3367 if (hugetlbfs_sanity_check(warn_on_failure, page_size)) { 3368 UseSHM = false; 3369 return true; 3370 } 3371 UseHugeTLBFS = false; 3372 } 3373 3374 return UseSHM; 3375 } 3376 3377 void os::large_page_init() { 3378 if (!UseLargePages && 3379 !UseTransparentHugePages && 3380 !UseHugeTLBFS && 3381 !UseSHM) { 3382 // Not using large pages. 3383 return; 3384 } 3385 3386 if (!FLAG_IS_DEFAULT(UseLargePages) && !UseLargePages) { 3387 // The user explicitly turned off large pages. 3388 // Ignore the rest of the large pages flags. 3389 UseTransparentHugePages = false; 3390 UseHugeTLBFS = false; 3391 UseSHM = false; 3392 return; 3393 } 3394 3395 size_t large_page_size = Linux::setup_large_page_size(); 3396 UseLargePages = Linux::setup_large_page_type(large_page_size); 3397 3398 set_coredump_filter(); 3399 } 3400 3401 #ifndef SHM_HUGETLB 3402 #define SHM_HUGETLB 04000 3403 #endif 3404 3405 #define shm_warning_format(format, ...) \ 3406 do { \ 3407 if (UseLargePages && \ 3408 (!FLAG_IS_DEFAULT(UseLargePages) || \ 3409 !FLAG_IS_DEFAULT(UseSHM) || \ 3410 !FLAG_IS_DEFAULT(LargePageSizeInBytes))) { \ 3411 warning(format, __VA_ARGS__); \ 3412 } \ 3413 } while (0) 3414 3415 #define shm_warning(str) shm_warning_format("%s", str) 3416 3417 #define shm_warning_with_errno(str) \ 3418 do { \ 3419 int err = errno; \ 3420 shm_warning_format(str " (error = %d)", err); \ 3421 } while (0) 3422 3423 static char* shmat_with_alignment(int shmid, size_t bytes, size_t alignment) { 3424 assert(is_size_aligned(bytes, alignment), "Must be divisible by the alignment"); 3425 3426 if (!is_size_aligned(alignment, SHMLBA)) { 3427 assert(false, "Code below assumes that alignment is at least SHMLBA aligned"); 3428 return NULL; 3429 } 3430 3431 // To ensure that we get 'alignment' aligned memory from shmat, 3432 // we pre-reserve aligned virtual memory and then attach to that. 3433 3434 char* pre_reserved_addr = anon_mmap_aligned(bytes, alignment, NULL); 3435 if (pre_reserved_addr == NULL) { 3436 // Couldn't pre-reserve aligned memory. 3437 shm_warning("Failed to pre-reserve aligned memory for shmat."); 3438 return NULL; 3439 } 3440 3441 // SHM_REMAP is needed to allow shmat to map over an existing mapping. 3442 char* addr = (char*)shmat(shmid, pre_reserved_addr, SHM_REMAP); 3443 3444 if ((intptr_t)addr == -1) { 3445 int err = errno; 3446 shm_warning_with_errno("Failed to attach shared memory."); 3447 3448 assert(err != EACCES, "Unexpected error"); 3449 assert(err != EIDRM, "Unexpected error"); 3450 assert(err != EINVAL, "Unexpected error"); 3451 3452 // Since we don't know if the kernel unmapped the pre-reserved memory area 3453 // we can't unmap it, since that would potentially unmap memory that was 3454 // mapped from other threads. 3455 return NULL; 3456 } 3457 3458 return addr; 3459 } 3460 3461 static char* shmat_at_address(int shmid, char* req_addr) { 3462 if (!is_ptr_aligned(req_addr, SHMLBA)) { 3463 assert(false, "Requested address needs to be SHMLBA aligned"); 3464 return NULL; 3465 } 3466 3467 char* addr = (char*)shmat(shmid, req_addr, 0); 3468 3469 if ((intptr_t)addr == -1) { 3470 shm_warning_with_errno("Failed to attach shared memory."); 3471 return NULL; 3472 } 3473 3474 return addr; 3475 } 3476 3477 static char* shmat_large_pages(int shmid, size_t bytes, size_t alignment, char* req_addr) { 3478 // If a req_addr has been provided, we assume that the caller has already aligned the address. 3479 if (req_addr != NULL) { 3480 assert(is_ptr_aligned(req_addr, os::large_page_size()), "Must be divisible by the large page size"); 3481 assert(is_ptr_aligned(req_addr, alignment), "Must be divisible by given alignment"); 3482 return shmat_at_address(shmid, req_addr); 3483 } 3484 3485 // Since shmid has been setup with SHM_HUGETLB, shmat will automatically 3486 // return large page size aligned memory addresses when req_addr == NULL. 3487 // However, if the alignment is larger than the large page size, we have 3488 // to manually ensure that the memory returned is 'alignment' aligned. 3489 if (alignment > os::large_page_size()) { 3490 assert(is_size_aligned(alignment, os::large_page_size()), "Must be divisible by the large page size"); 3491 return shmat_with_alignment(shmid, bytes, alignment); 3492 } else { 3493 return shmat_at_address(shmid, NULL); 3494 } 3495 } 3496 3497 char* os::Linux::reserve_memory_special_shm(size_t bytes, size_t alignment, 3498 char* req_addr, bool exec) { 3499 // "exec" is passed in but not used. Creating the shared image for 3500 // the code cache doesn't have an SHM_X executable permission to check. 3501 assert(UseLargePages && UseSHM, "only for SHM large pages"); 3502 assert(is_ptr_aligned(req_addr, os::large_page_size()), "Unaligned address"); 3503 assert(is_ptr_aligned(req_addr, alignment), "Unaligned address"); 3504 3505 if (!is_size_aligned(bytes, os::large_page_size())) { 3506 return NULL; // Fallback to small pages. 3507 } 3508 3509 // Create a large shared memory region to attach to based on size. 3510 // Currently, size is the total size of the heap. 3511 int shmid = shmget(IPC_PRIVATE, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W); 3512 if (shmid == -1) { 3513 // Possible reasons for shmget failure: 3514 // 1. shmmax is too small for Java heap. 3515 // > check shmmax value: cat /proc/sys/kernel/shmmax 3516 // > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax 3517 // 2. not enough large page memory. 3518 // > check available large pages: cat /proc/meminfo 3519 // > increase amount of large pages: 3520 // echo new_value > /proc/sys/vm/nr_hugepages 3521 // Note 1: different Linux may use different name for this property, 3522 // e.g. on Redhat AS-3 it is "hugetlb_pool". 3523 // Note 2: it's possible there's enough physical memory available but 3524 // they are so fragmented after a long run that they can't 3525 // coalesce into large pages. Try to reserve large pages when 3526 // the system is still "fresh". 3527 shm_warning_with_errno("Failed to reserve shared memory."); 3528 return NULL; 3529 } 3530 3531 // Attach to the region. 3532 char* addr = shmat_large_pages(shmid, bytes, alignment, req_addr); 3533 3534 // Remove shmid. If shmat() is successful, the actual shared memory segment 3535 // will be deleted when it's detached by shmdt() or when the process 3536 // terminates. If shmat() is not successful this will remove the shared 3537 // segment immediately. 3538 shmctl(shmid, IPC_RMID, NULL); 3539 3540 return addr; 3541 } 3542 3543 static void warn_on_large_pages_failure(char* req_addr, size_t bytes, 3544 int error) { 3545 assert(error == ENOMEM, "Only expect to fail if no memory is available"); 3546 3547 bool warn_on_failure = UseLargePages && 3548 (!FLAG_IS_DEFAULT(UseLargePages) || 3549 !FLAG_IS_DEFAULT(UseHugeTLBFS) || 3550 !FLAG_IS_DEFAULT(LargePageSizeInBytes)); 3551 3552 if (warn_on_failure) { 3553 char msg[128]; 3554 jio_snprintf(msg, sizeof(msg), "Failed to reserve large pages memory req_addr: " 3555 PTR_FORMAT " bytes: " SIZE_FORMAT " (errno = %d).", req_addr, bytes, error); 3556 warning("%s", msg); 3557 } 3558 } 3559 3560 char* os::Linux::reserve_memory_special_huge_tlbfs_only(size_t bytes, 3561 char* req_addr, 3562 bool exec) { 3563 assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages"); 3564 assert(is_size_aligned(bytes, os::large_page_size()), "Unaligned size"); 3565 assert(is_ptr_aligned(req_addr, os::large_page_size()), "Unaligned address"); 3566 3567 int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE; 3568 char* addr = (char*)::mmap(req_addr, bytes, prot, 3569 MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, 3570 -1, 0); 3571 3572 if (addr == MAP_FAILED) { 3573 warn_on_large_pages_failure(req_addr, bytes, errno); 3574 return NULL; 3575 } 3576 3577 assert(is_ptr_aligned(addr, os::large_page_size()), "Must be"); 3578 3579 return addr; 3580 } 3581 3582 // Reserve memory using mmap(MAP_HUGETLB). 3583 // - bytes shall be a multiple of alignment. 3584 // - req_addr can be NULL. If not NULL, it must be a multiple of alignment. 3585 // - alignment sets the alignment at which memory shall be allocated. 3586 // It must be a multiple of allocation granularity. 3587 // Returns address of memory or NULL. If req_addr was not NULL, will only return 3588 // req_addr or NULL. 3589 char* os::Linux::reserve_memory_special_huge_tlbfs_mixed(size_t bytes, 3590 size_t alignment, 3591 char* req_addr, 3592 bool exec) { 3593 size_t large_page_size = os::large_page_size(); 3594 assert(bytes >= large_page_size, "Shouldn't allocate large pages for small sizes"); 3595 3596 assert(is_ptr_aligned(req_addr, alignment), "Must be"); 3597 assert(is_size_aligned(bytes, alignment), "Must be"); 3598 3599 // First reserve - but not commit - the address range in small pages. 3600 char* const start = anon_mmap_aligned(bytes, alignment, req_addr); 3601 3602 if (start == NULL) { 3603 return NULL; 3604 } 3605 3606 assert(is_ptr_aligned(start, alignment), "Must be"); 3607 3608 char* end = start + bytes; 3609 3610 // Find the regions of the allocated chunk that can be promoted to large pages. 3611 char* lp_start = (char*)align_ptr_up(start, large_page_size); 3612 char* lp_end = (char*)align_ptr_down(end, large_page_size); 3613 3614 size_t lp_bytes = lp_end - lp_start; 3615 3616 assert(is_size_aligned(lp_bytes, large_page_size), "Must be"); 3617 3618 if (lp_bytes == 0) { 3619 // The mapped region doesn't even span the start and the end of a large page. 3620 // Fall back to allocate a non-special area. 3621 ::munmap(start, end - start); 3622 return NULL; 3623 } 3624 3625 int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE; 3626 3627 void* result; 3628 3629 // Commit small-paged leading area. 3630 if (start != lp_start) { 3631 result = ::mmap(start, lp_start - start, prot, 3632 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 3633 -1, 0); 3634 if (result == MAP_FAILED) { 3635 ::munmap(lp_start, end - lp_start); 3636 return NULL; 3637 } 3638 } 3639 3640 // Commit large-paged area. 3641 result = ::mmap(lp_start, lp_bytes, prot, 3642 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_HUGETLB, 3643 -1, 0); 3644 if (result == MAP_FAILED) { 3645 warn_on_large_pages_failure(lp_start, lp_bytes, errno); 3646 // If the mmap above fails, the large pages region will be unmapped and we 3647 // have regions before and after with small pages. Release these regions. 3648 // 3649 // | mapped | unmapped | mapped | 3650 // ^ ^ ^ ^ 3651 // start lp_start lp_end end 3652 // 3653 ::munmap(start, lp_start - start); 3654 ::munmap(lp_end, end - lp_end); 3655 return NULL; 3656 } 3657 3658 // Commit small-paged trailing area. 3659 if (lp_end != end) { 3660 result = ::mmap(lp_end, end - lp_end, prot, 3661 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 3662 -1, 0); 3663 if (result == MAP_FAILED) { 3664 ::munmap(start, lp_end - start); 3665 return NULL; 3666 } 3667 } 3668 3669 return start; 3670 } 3671 3672 char* os::Linux::reserve_memory_special_huge_tlbfs(size_t bytes, 3673 size_t alignment, 3674 char* req_addr, 3675 bool exec) { 3676 assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages"); 3677 assert(is_ptr_aligned(req_addr, alignment), "Must be"); 3678 assert(is_size_aligned(alignment, os::vm_allocation_granularity()), "Must be"); 3679 assert(is_power_of_2(os::large_page_size()), "Must be"); 3680 assert(bytes >= os::large_page_size(), "Shouldn't allocate large pages for small sizes"); 3681 3682 if (is_size_aligned(bytes, os::large_page_size()) && alignment <= os::large_page_size()) { 3683 return reserve_memory_special_huge_tlbfs_only(bytes, req_addr, exec); 3684 } else { 3685 return reserve_memory_special_huge_tlbfs_mixed(bytes, alignment, req_addr, exec); 3686 } 3687 } 3688 3689 char* os::reserve_memory_special(size_t bytes, size_t alignment, 3690 char* req_addr, bool exec) { 3691 assert(UseLargePages, "only for large pages"); 3692 3693 char* addr; 3694 if (UseSHM) { 3695 addr = os::Linux::reserve_memory_special_shm(bytes, alignment, req_addr, exec); 3696 } else { 3697 assert(UseHugeTLBFS, "must be"); 3698 addr = os::Linux::reserve_memory_special_huge_tlbfs(bytes, alignment, req_addr, exec); 3699 } 3700 3701 if (addr != NULL) { 3702 if (UseNUMAInterleaving) { 3703 numa_make_global(addr, bytes); 3704 } 3705 3706 // The memory is committed 3707 MemTracker::record_virtual_memory_reserve_and_commit((address)addr, bytes, CALLER_PC); 3708 } 3709 3710 return addr; 3711 } 3712 3713 bool os::Linux::release_memory_special_shm(char* base, size_t bytes) { 3714 // detaching the SHM segment will also delete it, see reserve_memory_special_shm() 3715 return shmdt(base) == 0; 3716 } 3717 3718 bool os::Linux::release_memory_special_huge_tlbfs(char* base, size_t bytes) { 3719 return pd_release_memory(base, bytes); 3720 } 3721 3722 bool os::release_memory_special(char* base, size_t bytes) { 3723 bool res; 3724 if (MemTracker::tracking_level() > NMT_minimal) { 3725 Tracker tkr = MemTracker::get_virtual_memory_release_tracker(); 3726 res = os::Linux::release_memory_special_impl(base, bytes); 3727 if (res) { 3728 tkr.record((address)base, bytes); 3729 } 3730 3731 } else { 3732 res = os::Linux::release_memory_special_impl(base, bytes); 3733 } 3734 return res; 3735 } 3736 3737 bool os::Linux::release_memory_special_impl(char* base, size_t bytes) { 3738 assert(UseLargePages, "only for large pages"); 3739 bool res; 3740 3741 if (UseSHM) { 3742 res = os::Linux::release_memory_special_shm(base, bytes); 3743 } else { 3744 assert(UseHugeTLBFS, "must be"); 3745 res = os::Linux::release_memory_special_huge_tlbfs(base, bytes); 3746 } 3747 return res; 3748 } 3749 3750 size_t os::large_page_size() { 3751 return _large_page_size; 3752 } 3753 3754 // With SysV SHM the entire memory region must be allocated as shared 3755 // memory. 3756 // HugeTLBFS allows application to commit large page memory on demand. 3757 // However, when committing memory with HugeTLBFS fails, the region 3758 // that was supposed to be committed will lose the old reservation 3759 // and allow other threads to steal that memory region. Because of this 3760 // behavior we can't commit HugeTLBFS memory. 3761 bool os::can_commit_large_page_memory() { 3762 return UseTransparentHugePages; 3763 } 3764 3765 bool os::can_execute_large_page_memory() { 3766 return UseTransparentHugePages || UseHugeTLBFS; 3767 } 3768 3769 // Reserve memory at an arbitrary address, only if that area is 3770 // available (and not reserved for something else). 3771 3772 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) { 3773 const int max_tries = 10; 3774 char* base[max_tries]; 3775 size_t size[max_tries]; 3776 const size_t gap = 0x000000; 3777 3778 // Assert only that the size is a multiple of the page size, since 3779 // that's all that mmap requires, and since that's all we really know 3780 // about at this low abstraction level. If we need higher alignment, 3781 // we can either pass an alignment to this method or verify alignment 3782 // in one of the methods further up the call chain. See bug 5044738. 3783 assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block"); 3784 3785 // Repeatedly allocate blocks until the block is allocated at the 3786 // right spot. 3787 3788 // Linux mmap allows caller to pass an address as hint; give it a try first, 3789 // if kernel honors the hint then we can return immediately. 3790 char * addr = anon_mmap(requested_addr, bytes, false); 3791 if (addr == requested_addr) { 3792 return requested_addr; 3793 } 3794 3795 if (addr != NULL) { 3796 // mmap() is successful but it fails to reserve at the requested address 3797 anon_munmap(addr, bytes); 3798 } 3799 3800 int i; 3801 for (i = 0; i < max_tries; ++i) { 3802 base[i] = reserve_memory(bytes); 3803 3804 if (base[i] != NULL) { 3805 // Is this the block we wanted? 3806 if (base[i] == requested_addr) { 3807 size[i] = bytes; 3808 break; 3809 } 3810 3811 // Does this overlap the block we wanted? Give back the overlapped 3812 // parts and try again. 3813 3814 ptrdiff_t top_overlap = requested_addr + (bytes + gap) - base[i]; 3815 if (top_overlap >= 0 && (size_t)top_overlap < bytes) { 3816 unmap_memory(base[i], top_overlap); 3817 base[i] += top_overlap; 3818 size[i] = bytes - top_overlap; 3819 } else { 3820 ptrdiff_t bottom_overlap = base[i] + bytes - requested_addr; 3821 if (bottom_overlap >= 0 && (size_t)bottom_overlap < bytes) { 3822 unmap_memory(requested_addr, bottom_overlap); 3823 size[i] = bytes - bottom_overlap; 3824 } else { 3825 size[i] = bytes; 3826 } 3827 } 3828 } 3829 } 3830 3831 // Give back the unused reserved pieces. 3832 3833 for (int j = 0; j < i; ++j) { 3834 if (base[j] != NULL) { 3835 unmap_memory(base[j], size[j]); 3836 } 3837 } 3838 3839 if (i < max_tries) { 3840 return requested_addr; 3841 } else { 3842 return NULL; 3843 } 3844 } 3845 3846 size_t os::read(int fd, void *buf, unsigned int nBytes) { 3847 return ::read(fd, buf, nBytes); 3848 } 3849 3850 size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) { 3851 return ::pread(fd, buf, nBytes, offset); 3852 } 3853 3854 // Short sleep, direct OS call. 3855 // 3856 // Note: certain versions of Linux CFS scheduler (since 2.6.23) do not guarantee 3857 // sched_yield(2) will actually give up the CPU: 3858 // 3859 // * Alone on this pariticular CPU, keeps running. 3860 // * Before the introduction of "skip_buddy" with "compat_yield" disabled 3861 // (pre 2.6.39). 3862 // 3863 // So calling this with 0 is an alternative. 3864 // 3865 void os::naked_short_sleep(jlong ms) { 3866 struct timespec req; 3867 3868 assert(ms < 1000, "Un-interruptable sleep, short time use only"); 3869 req.tv_sec = 0; 3870 if (ms > 0) { 3871 req.tv_nsec = (ms % 1000) * 1000000; 3872 } else { 3873 req.tv_nsec = 1; 3874 } 3875 3876 nanosleep(&req, NULL); 3877 3878 return; 3879 } 3880 3881 // Sleep forever; naked call to OS-specific sleep; use with CAUTION 3882 void os::infinite_sleep() { 3883 while (true) { // sleep forever ... 3884 ::sleep(100); // ... 100 seconds at a time 3885 } 3886 } 3887 3888 // Used to convert frequent JVM_Yield() to nops 3889 bool os::dont_yield() { 3890 return DontYieldALot; 3891 } 3892 3893 void os::naked_yield() { 3894 sched_yield(); 3895 } 3896 3897 //////////////////////////////////////////////////////////////////////////////// 3898 // thread priority support 3899 3900 // Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER 3901 // only supports dynamic priority, static priority must be zero. For real-time 3902 // applications, Linux supports SCHED_RR which allows static priority (1-99). 3903 // However, for large multi-threaded applications, SCHED_RR is not only slower 3904 // than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out 3905 // of 5 runs - Sep 2005). 3906 // 3907 // The following code actually changes the niceness of kernel-thread/LWP. It 3908 // has an assumption that setpriority() only modifies one kernel-thread/LWP, 3909 // not the entire user process, and user level threads are 1:1 mapped to kernel 3910 // threads. It has always been the case, but could change in the future. For 3911 // this reason, the code should not be used as default (ThreadPriorityPolicy=0). 3912 // It is only used when ThreadPriorityPolicy=1 and requires root privilege. 3913 3914 int os::java_to_os_priority[CriticalPriority + 1] = { 3915 19, // 0 Entry should never be used 3916 3917 4, // 1 MinPriority 3918 3, // 2 3919 2, // 3 3920 3921 1, // 4 3922 0, // 5 NormPriority 3923 -1, // 6 3924 3925 -2, // 7 3926 -3, // 8 3927 -4, // 9 NearMaxPriority 3928 3929 -5, // 10 MaxPriority 3930 3931 -5 // 11 CriticalPriority 3932 }; 3933 3934 static int prio_init() { 3935 if (ThreadPriorityPolicy == 1) { 3936 // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1 3937 // if effective uid is not root. Perhaps, a more elegant way of doing 3938 // this is to test CAP_SYS_NICE capability, but that will require libcap.so 3939 if (geteuid() != 0) { 3940 if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) { 3941 warning("-XX:ThreadPriorityPolicy requires root privilege on Linux"); 3942 } 3943 ThreadPriorityPolicy = 0; 3944 } 3945 } 3946 if (UseCriticalJavaThreadPriority) { 3947 os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority]; 3948 } 3949 return 0; 3950 } 3951 3952 OSReturn os::set_native_priority(Thread* thread, int newpri) { 3953 if (!UseThreadPriorities || ThreadPriorityPolicy == 0) return OS_OK; 3954 3955 int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri); 3956 return (ret == 0) ? OS_OK : OS_ERR; 3957 } 3958 3959 OSReturn os::get_native_priority(const Thread* const thread, 3960 int *priority_ptr) { 3961 if (!UseThreadPriorities || ThreadPriorityPolicy == 0) { 3962 *priority_ptr = java_to_os_priority[NormPriority]; 3963 return OS_OK; 3964 } 3965 3966 errno = 0; 3967 *priority_ptr = getpriority(PRIO_PROCESS, thread->osthread()->thread_id()); 3968 return (*priority_ptr != -1 || errno == 0 ? OS_OK : OS_ERR); 3969 } 3970 3971 // Hint to the underlying OS that a task switch would not be good. 3972 // Void return because it's a hint and can fail. 3973 void os::hint_no_preempt() {} 3974 3975 //////////////////////////////////////////////////////////////////////////////// 3976 // suspend/resume support 3977 3978 // the low-level signal-based suspend/resume support is a remnant from the 3979 // old VM-suspension that used to be for java-suspension, safepoints etc, 3980 // within hotspot. Now there is a single use-case for this: 3981 // - calling get_thread_pc() on the VMThread by the flat-profiler task 3982 // that runs in the watcher thread. 3983 // The remaining code is greatly simplified from the more general suspension 3984 // code that used to be used. 3985 // 3986 // The protocol is quite simple: 3987 // - suspend: 3988 // - sends a signal to the target thread 3989 // - polls the suspend state of the osthread using a yield loop 3990 // - target thread signal handler (SR_handler) sets suspend state 3991 // and blocks in sigsuspend until continued 3992 // - resume: 3993 // - sets target osthread state to continue 3994 // - sends signal to end the sigsuspend loop in the SR_handler 3995 // 3996 // Note that the SR_lock plays no role in this suspend/resume protocol, 3997 // but is checked for NULL in SR_handler as a thread termination indicator. 3998 3999 static void resume_clear_context(OSThread *osthread) { 4000 osthread->set_ucontext(NULL); 4001 osthread->set_siginfo(NULL); 4002 } 4003 4004 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, 4005 ucontext_t* context) { 4006 osthread->set_ucontext(context); 4007 osthread->set_siginfo(siginfo); 4008 } 4009 4010 // Handler function invoked when a thread's execution is suspended or 4011 // resumed. We have to be careful that only async-safe functions are 4012 // called here (Note: most pthread functions are not async safe and 4013 // should be avoided.) 4014 // 4015 // Note: sigwait() is a more natural fit than sigsuspend() from an 4016 // interface point of view, but sigwait() prevents the signal hander 4017 // from being run. libpthread would get very confused by not having 4018 // its signal handlers run and prevents sigwait()'s use with the 4019 // mutex granting granting signal. 4020 // 4021 // Currently only ever called on the VMThread and JavaThreads (PC sampling) 4022 // 4023 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) { 4024 // Save and restore errno to avoid confusing native code with EINTR 4025 // after sigsuspend. 4026 int old_errno = errno; 4027 4028 Thread* thread = Thread::current_or_null_safe(); 4029 assert(thread != NULL, "Missing current thread in SR_handler"); 4030 4031 // On some systems we have seen signal delivery get "stuck" until the signal 4032 // mask is changed as part of thread termination. Check that the current thread 4033 // has not already terminated (via SR_lock()) - else the following assertion 4034 // will fail because the thread is no longer a JavaThread as the ~JavaThread 4035 // destructor has completed. 4036 4037 if (thread->SR_lock() == NULL) { 4038 return; 4039 } 4040 4041 assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread"); 4042 4043 OSThread* osthread = thread->osthread(); 4044 4045 os::SuspendResume::State current = osthread->sr.state(); 4046 if (current == os::SuspendResume::SR_SUSPEND_REQUEST) { 4047 suspend_save_context(osthread, siginfo, context); 4048 4049 // attempt to switch the state, we assume we had a SUSPEND_REQUEST 4050 os::SuspendResume::State state = osthread->sr.suspended(); 4051 if (state == os::SuspendResume::SR_SUSPENDED) { 4052 sigset_t suspend_set; // signals for sigsuspend() 4053 sigemptyset(&suspend_set); 4054 // get current set of blocked signals and unblock resume signal 4055 pthread_sigmask(SIG_BLOCK, NULL, &suspend_set); 4056 sigdelset(&suspend_set, SR_signum); 4057 4058 sr_semaphore.signal(); 4059 // wait here until we are resumed 4060 while (1) { 4061 sigsuspend(&suspend_set); 4062 4063 os::SuspendResume::State result = osthread->sr.running(); 4064 if (result == os::SuspendResume::SR_RUNNING) { 4065 sr_semaphore.signal(); 4066 break; 4067 } 4068 } 4069 4070 } else if (state == os::SuspendResume::SR_RUNNING) { 4071 // request was cancelled, continue 4072 } else { 4073 ShouldNotReachHere(); 4074 } 4075 4076 resume_clear_context(osthread); 4077 } else if (current == os::SuspendResume::SR_RUNNING) { 4078 // request was cancelled, continue 4079 } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) { 4080 // ignore 4081 } else { 4082 // ignore 4083 } 4084 4085 errno = old_errno; 4086 } 4087 4088 static int SR_initialize() { 4089 struct sigaction act; 4090 char *s; 4091 4092 // Get signal number to use for suspend/resume 4093 if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) { 4094 int sig = ::strtol(s, 0, 10); 4095 if (sig > MAX2(SIGSEGV, SIGBUS) && // See 4355769. 4096 sig < NSIG) { // Must be legal signal and fit into sigflags[]. 4097 SR_signum = sig; 4098 } else { 4099 warning("You set _JAVA_SR_SIGNUM=%d. It must be in range [%d, %d]. Using %d instead.", 4100 sig, MAX2(SIGSEGV, SIGBUS)+1, NSIG-1, SR_signum); 4101 } 4102 } 4103 4104 assert(SR_signum > SIGSEGV && SR_signum > SIGBUS, 4105 "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769"); 4106 4107 sigemptyset(&SR_sigset); 4108 sigaddset(&SR_sigset, SR_signum); 4109 4110 // Set up signal handler for suspend/resume 4111 act.sa_flags = SA_RESTART|SA_SIGINFO; 4112 act.sa_handler = (void (*)(int)) SR_handler; 4113 4114 // SR_signum is blocked by default. 4115 // 4528190 - We also need to block pthread restart signal (32 on all 4116 // supported Linux platforms). Note that LinuxThreads need to block 4117 // this signal for all threads to work properly. So we don't have 4118 // to use hard-coded signal number when setting up the mask. 4119 pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask); 4120 4121 if (sigaction(SR_signum, &act, 0) == -1) { 4122 return -1; 4123 } 4124 4125 // Save signal flag 4126 os::Linux::set_our_sigflags(SR_signum, act.sa_flags); 4127 return 0; 4128 } 4129 4130 static int sr_notify(OSThread* osthread) { 4131 int status = pthread_kill(osthread->pthread_id(), SR_signum); 4132 assert_status(status == 0, status, "pthread_kill"); 4133 return status; 4134 } 4135 4136 // "Randomly" selected value for how long we want to spin 4137 // before bailing out on suspending a thread, also how often 4138 // we send a signal to a thread we want to resume 4139 static const int RANDOMLY_LARGE_INTEGER = 1000000; 4140 static const int RANDOMLY_LARGE_INTEGER2 = 100; 4141 4142 // returns true on success and false on error - really an error is fatal 4143 // but this seems the normal response to library errors 4144 static bool do_suspend(OSThread* osthread) { 4145 assert(osthread->sr.is_running(), "thread should be running"); 4146 assert(!sr_semaphore.trywait(), "semaphore has invalid state"); 4147 4148 // mark as suspended and send signal 4149 if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) { 4150 // failed to switch, state wasn't running? 4151 ShouldNotReachHere(); 4152 return false; 4153 } 4154 4155 if (sr_notify(osthread) != 0) { 4156 ShouldNotReachHere(); 4157 } 4158 4159 // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED 4160 while (true) { 4161 if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) { 4162 break; 4163 } else { 4164 // timeout 4165 os::SuspendResume::State cancelled = osthread->sr.cancel_suspend(); 4166 if (cancelled == os::SuspendResume::SR_RUNNING) { 4167 return false; 4168 } else if (cancelled == os::SuspendResume::SR_SUSPENDED) { 4169 // make sure that we consume the signal on the semaphore as well 4170 sr_semaphore.wait(); 4171 break; 4172 } else { 4173 ShouldNotReachHere(); 4174 return false; 4175 } 4176 } 4177 } 4178 4179 guarantee(osthread->sr.is_suspended(), "Must be suspended"); 4180 return true; 4181 } 4182 4183 static void do_resume(OSThread* osthread) { 4184 assert(osthread->sr.is_suspended(), "thread should be suspended"); 4185 assert(!sr_semaphore.trywait(), "invalid semaphore state"); 4186 4187 if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) { 4188 // failed to switch to WAKEUP_REQUEST 4189 ShouldNotReachHere(); 4190 return; 4191 } 4192 4193 while (true) { 4194 if (sr_notify(osthread) == 0) { 4195 if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) { 4196 if (osthread->sr.is_running()) { 4197 return; 4198 } 4199 } 4200 } else { 4201 ShouldNotReachHere(); 4202 } 4203 } 4204 4205 guarantee(osthread->sr.is_running(), "Must be running!"); 4206 } 4207 4208 /////////////////////////////////////////////////////////////////////////////////// 4209 // signal handling (except suspend/resume) 4210 4211 // This routine may be used by user applications as a "hook" to catch signals. 4212 // The user-defined signal handler must pass unrecognized signals to this 4213 // routine, and if it returns true (non-zero), then the signal handler must 4214 // return immediately. If the flag "abort_if_unrecognized" is true, then this 4215 // routine will never retun false (zero), but instead will execute a VM panic 4216 // routine kill the process. 4217 // 4218 // If this routine returns false, it is OK to call it again. This allows 4219 // the user-defined signal handler to perform checks either before or after 4220 // the VM performs its own checks. Naturally, the user code would be making 4221 // a serious error if it tried to handle an exception (such as a null check 4222 // or breakpoint) that the VM was generating for its own correct operation. 4223 // 4224 // This routine may recognize any of the following kinds of signals: 4225 // SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1. 4226 // It should be consulted by handlers for any of those signals. 4227 // 4228 // The caller of this routine must pass in the three arguments supplied 4229 // to the function referred to in the "sa_sigaction" (not the "sa_handler") 4230 // field of the structure passed to sigaction(). This routine assumes that 4231 // the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART. 4232 // 4233 // Note that the VM will print warnings if it detects conflicting signal 4234 // handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers". 4235 // 4236 extern "C" JNIEXPORT int JVM_handle_linux_signal(int signo, 4237 siginfo_t* siginfo, 4238 void* ucontext, 4239 int abort_if_unrecognized); 4240 4241 void signalHandler(int sig, siginfo_t* info, void* uc) { 4242 assert(info != NULL && uc != NULL, "it must be old kernel"); 4243 int orig_errno = errno; // Preserve errno value over signal handler. 4244 JVM_handle_linux_signal(sig, info, uc, true); 4245 errno = orig_errno; 4246 } 4247 4248 4249 // This boolean allows users to forward their own non-matching signals 4250 // to JVM_handle_linux_signal, harmlessly. 4251 bool os::Linux::signal_handlers_are_installed = false; 4252 4253 // For signal-chaining 4254 struct sigaction sigact[NSIG]; 4255 uint64_t sigs = 0; 4256 #if (64 < NSIG-1) 4257 #error "Not all signals can be encoded in sigs. Adapt its type!" 4258 #endif 4259 bool os::Linux::libjsig_is_loaded = false; 4260 typedef struct sigaction *(*get_signal_t)(int); 4261 get_signal_t os::Linux::get_signal_action = NULL; 4262 4263 struct sigaction* os::Linux::get_chained_signal_action(int sig) { 4264 struct sigaction *actp = NULL; 4265 4266 if (libjsig_is_loaded) { 4267 // Retrieve the old signal handler from libjsig 4268 actp = (*get_signal_action)(sig); 4269 } 4270 if (actp == NULL) { 4271 // Retrieve the preinstalled signal handler from jvm 4272 actp = get_preinstalled_handler(sig); 4273 } 4274 4275 return actp; 4276 } 4277 4278 static bool call_chained_handler(struct sigaction *actp, int sig, 4279 siginfo_t *siginfo, void *context) { 4280 // Call the old signal handler 4281 if (actp->sa_handler == SIG_DFL) { 4282 // It's more reasonable to let jvm treat it as an unexpected exception 4283 // instead of taking the default action. 4284 return false; 4285 } else if (actp->sa_handler != SIG_IGN) { 4286 if ((actp->sa_flags & SA_NODEFER) == 0) { 4287 // automaticlly block the signal 4288 sigaddset(&(actp->sa_mask), sig); 4289 } 4290 4291 sa_handler_t hand = NULL; 4292 sa_sigaction_t sa = NULL; 4293 bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0; 4294 // retrieve the chained handler 4295 if (siginfo_flag_set) { 4296 sa = actp->sa_sigaction; 4297 } else { 4298 hand = actp->sa_handler; 4299 } 4300 4301 if ((actp->sa_flags & SA_RESETHAND) != 0) { 4302 actp->sa_handler = SIG_DFL; 4303 } 4304 4305 // try to honor the signal mask 4306 sigset_t oset; 4307 sigemptyset(&oset); 4308 pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset); 4309 4310 // call into the chained handler 4311 if (siginfo_flag_set) { 4312 (*sa)(sig, siginfo, context); 4313 } else { 4314 (*hand)(sig); 4315 } 4316 4317 // restore the signal mask 4318 pthread_sigmask(SIG_SETMASK, &oset, NULL); 4319 } 4320 // Tell jvm's signal handler the signal is taken care of. 4321 return true; 4322 } 4323 4324 bool os::Linux::chained_handler(int sig, siginfo_t* siginfo, void* context) { 4325 bool chained = false; 4326 // signal-chaining 4327 if (UseSignalChaining) { 4328 struct sigaction *actp = get_chained_signal_action(sig); 4329 if (actp != NULL) { 4330 chained = call_chained_handler(actp, sig, siginfo, context); 4331 } 4332 } 4333 return chained; 4334 } 4335 4336 struct sigaction* os::Linux::get_preinstalled_handler(int sig) { 4337 if ((((uint64_t)1 << (sig-1)) & sigs) != 0) { 4338 return &sigact[sig]; 4339 } 4340 return NULL; 4341 } 4342 4343 void os::Linux::save_preinstalled_handler(int sig, struct sigaction& oldAct) { 4344 assert(sig > 0 && sig < NSIG, "vm signal out of expected range"); 4345 sigact[sig] = oldAct; 4346 sigs |= (uint64_t)1 << (sig-1); 4347 } 4348 4349 // for diagnostic 4350 int sigflags[NSIG]; 4351 4352 int os::Linux::get_our_sigflags(int sig) { 4353 assert(sig > 0 && sig < NSIG, "vm signal out of expected range"); 4354 return sigflags[sig]; 4355 } 4356 4357 void os::Linux::set_our_sigflags(int sig, int flags) { 4358 assert(sig > 0 && sig < NSIG, "vm signal out of expected range"); 4359 if (sig > 0 && sig < NSIG) { 4360 sigflags[sig] = flags; 4361 } 4362 } 4363 4364 void os::Linux::set_signal_handler(int sig, bool set_installed) { 4365 // Check for overwrite. 4366 struct sigaction oldAct; 4367 sigaction(sig, (struct sigaction*)NULL, &oldAct); 4368 4369 void* oldhand = oldAct.sa_sigaction 4370 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 4371 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 4372 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) && 4373 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) && 4374 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) { 4375 if (AllowUserSignalHandlers || !set_installed) { 4376 // Do not overwrite; user takes responsibility to forward to us. 4377 return; 4378 } else if (UseSignalChaining) { 4379 // save the old handler in jvm 4380 save_preinstalled_handler(sig, oldAct); 4381 // libjsig also interposes the sigaction() call below and saves the 4382 // old sigaction on it own. 4383 } else { 4384 fatal("Encountered unexpected pre-existing sigaction handler " 4385 "%#lx for signal %d.", (long)oldhand, sig); 4386 } 4387 } 4388 4389 struct sigaction sigAct; 4390 sigfillset(&(sigAct.sa_mask)); 4391 sigAct.sa_handler = SIG_DFL; 4392 if (!set_installed) { 4393 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 4394 } else { 4395 sigAct.sa_sigaction = signalHandler; 4396 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 4397 } 4398 // Save flags, which are set by ours 4399 assert(sig > 0 && sig < NSIG, "vm signal out of expected range"); 4400 sigflags[sig] = sigAct.sa_flags; 4401 4402 int ret = sigaction(sig, &sigAct, &oldAct); 4403 assert(ret == 0, "check"); 4404 4405 void* oldhand2 = oldAct.sa_sigaction 4406 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 4407 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 4408 assert(oldhand2 == oldhand, "no concurrent signal handler installation"); 4409 } 4410 4411 // install signal handlers for signals that HotSpot needs to 4412 // handle in order to support Java-level exception handling. 4413 4414 void os::Linux::install_signal_handlers() { 4415 if (!signal_handlers_are_installed) { 4416 signal_handlers_are_installed = true; 4417 4418 // signal-chaining 4419 typedef void (*signal_setting_t)(); 4420 signal_setting_t begin_signal_setting = NULL; 4421 signal_setting_t end_signal_setting = NULL; 4422 begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t, 4423 dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting")); 4424 if (begin_signal_setting != NULL) { 4425 end_signal_setting = CAST_TO_FN_PTR(signal_setting_t, 4426 dlsym(RTLD_DEFAULT, "JVM_end_signal_setting")); 4427 get_signal_action = CAST_TO_FN_PTR(get_signal_t, 4428 dlsym(RTLD_DEFAULT, "JVM_get_signal_action")); 4429 libjsig_is_loaded = true; 4430 assert(UseSignalChaining, "should enable signal-chaining"); 4431 } 4432 if (libjsig_is_loaded) { 4433 // Tell libjsig jvm is setting signal handlers 4434 (*begin_signal_setting)(); 4435 } 4436 4437 set_signal_handler(SIGSEGV, true); 4438 set_signal_handler(SIGPIPE, true); 4439 set_signal_handler(SIGBUS, true); 4440 set_signal_handler(SIGILL, true); 4441 set_signal_handler(SIGFPE, true); 4442 #if defined(PPC64) 4443 set_signal_handler(SIGTRAP, true); 4444 #endif 4445 set_signal_handler(SIGXFSZ, true); 4446 4447 if (libjsig_is_loaded) { 4448 // Tell libjsig jvm finishes setting signal handlers 4449 (*end_signal_setting)(); 4450 } 4451 4452 // We don't activate signal checker if libjsig is in place, we trust ourselves 4453 // and if UserSignalHandler is installed all bets are off. 4454 // Log that signal checking is off only if -verbose:jni is specified. 4455 if (CheckJNICalls) { 4456 if (libjsig_is_loaded) { 4457 if (PrintJNIResolving) { 4458 tty->print_cr("Info: libjsig is activated, all active signal checking is disabled"); 4459 } 4460 check_signals = false; 4461 } 4462 if (AllowUserSignalHandlers) { 4463 if (PrintJNIResolving) { 4464 tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled"); 4465 } 4466 check_signals = false; 4467 } 4468 } 4469 } 4470 } 4471 4472 // This is the fastest way to get thread cpu time on Linux. 4473 // Returns cpu time (user+sys) for any thread, not only for current. 4474 // POSIX compliant clocks are implemented in the kernels 2.6.16+. 4475 // It might work on 2.6.10+ with a special kernel/glibc patch. 4476 // For reference, please, see IEEE Std 1003.1-2004: 4477 // http://www.unix.org/single_unix_specification 4478 4479 jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) { 4480 struct timespec tp; 4481 int rc = os::Linux::clock_gettime(clockid, &tp); 4482 assert(rc == 0, "clock_gettime is expected to return 0 code"); 4483 4484 return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec; 4485 } 4486 4487 void os::Linux::initialize_os_info() { 4488 assert(_os_version == 0, "OS info already initialized"); 4489 4490 struct utsname _uname; 4491 4492 uint32_t major; 4493 uint32_t minor; 4494 uint32_t fix; 4495 4496 int rc; 4497 4498 // Kernel version is unknown if 4499 // verification below fails. 4500 _os_version = 0x01000000; 4501 4502 rc = uname(&_uname); 4503 if (rc != -1) { 4504 4505 rc = sscanf(_uname.release,"%d.%d.%d", &major, &minor, &fix); 4506 if (rc == 3) { 4507 4508 if (major < 256 && minor < 256 && fix < 256) { 4509 // Kernel version format is as expected, 4510 // set it overriding unknown state. 4511 _os_version = (major << 16) | 4512 (minor << 8 ) | 4513 (fix << 0 ) ; 4514 } 4515 } 4516 } 4517 } 4518 4519 uint32_t os::Linux::os_version() { 4520 assert(_os_version != 0, "not initialized"); 4521 return _os_version & 0x00FFFFFF; 4522 } 4523 4524 bool os::Linux::os_version_is_known() { 4525 assert(_os_version != 0, "not initialized"); 4526 return _os_version & 0x01000000 ? false : true; 4527 } 4528 4529 ///// 4530 // glibc on Linux platform uses non-documented flag 4531 // to indicate, that some special sort of signal 4532 // trampoline is used. 4533 // We will never set this flag, and we should 4534 // ignore this flag in our diagnostic 4535 #ifdef SIGNIFICANT_SIGNAL_MASK 4536 #undef SIGNIFICANT_SIGNAL_MASK 4537 #endif 4538 #define SIGNIFICANT_SIGNAL_MASK (~0x04000000) 4539 4540 static const char* get_signal_handler_name(address handler, 4541 char* buf, int buflen) { 4542 int offset = 0; 4543 bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset); 4544 if (found) { 4545 // skip directory names 4546 const char *p1, *p2; 4547 p1 = buf; 4548 size_t len = strlen(os::file_separator()); 4549 while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len; 4550 jio_snprintf(buf, buflen, "%s+0x%x", p1, offset); 4551 } else { 4552 jio_snprintf(buf, buflen, PTR_FORMAT, handler); 4553 } 4554 return buf; 4555 } 4556 4557 static void print_signal_handler(outputStream* st, int sig, 4558 char* buf, size_t buflen) { 4559 struct sigaction sa; 4560 4561 sigaction(sig, NULL, &sa); 4562 4563 // See comment for SIGNIFICANT_SIGNAL_MASK define 4564 sa.sa_flags &= SIGNIFICANT_SIGNAL_MASK; 4565 4566 st->print("%s: ", os::exception_name(sig, buf, buflen)); 4567 4568 address handler = (sa.sa_flags & SA_SIGINFO) 4569 ? CAST_FROM_FN_PTR(address, sa.sa_sigaction) 4570 : CAST_FROM_FN_PTR(address, sa.sa_handler); 4571 4572 if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) { 4573 st->print("SIG_DFL"); 4574 } else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) { 4575 st->print("SIG_IGN"); 4576 } else { 4577 st->print("[%s]", get_signal_handler_name(handler, buf, buflen)); 4578 } 4579 4580 st->print(", sa_mask[0]="); 4581 os::Posix::print_signal_set_short(st, &sa.sa_mask); 4582 4583 address rh = VMError::get_resetted_sighandler(sig); 4584 // May be, handler was resetted by VMError? 4585 if (rh != NULL) { 4586 handler = rh; 4587 sa.sa_flags = VMError::get_resetted_sigflags(sig) & SIGNIFICANT_SIGNAL_MASK; 4588 } 4589 4590 st->print(", sa_flags="); 4591 os::Posix::print_sa_flags(st, sa.sa_flags); 4592 4593 // Check: is it our handler? 4594 if (handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler) || 4595 handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler)) { 4596 // It is our signal handler 4597 // check for flags, reset system-used one! 4598 if ((int)sa.sa_flags != os::Linux::get_our_sigflags(sig)) { 4599 st->print( 4600 ", flags was changed from " PTR32_FORMAT ", consider using jsig library", 4601 os::Linux::get_our_sigflags(sig)); 4602 } 4603 } 4604 st->cr(); 4605 } 4606 4607 4608 #define DO_SIGNAL_CHECK(sig) \ 4609 do { \ 4610 if (!sigismember(&check_signal_done, sig)) { \ 4611 os::Linux::check_signal_handler(sig); \ 4612 } \ 4613 } while (0) 4614 4615 // This method is a periodic task to check for misbehaving JNI applications 4616 // under CheckJNI, we can add any periodic checks here 4617 4618 void os::run_periodic_checks() { 4619 if (check_signals == false) return; 4620 4621 // SEGV and BUS if overridden could potentially prevent 4622 // generation of hs*.log in the event of a crash, debugging 4623 // such a case can be very challenging, so we absolutely 4624 // check the following for a good measure: 4625 DO_SIGNAL_CHECK(SIGSEGV); 4626 DO_SIGNAL_CHECK(SIGILL); 4627 DO_SIGNAL_CHECK(SIGFPE); 4628 DO_SIGNAL_CHECK(SIGBUS); 4629 DO_SIGNAL_CHECK(SIGPIPE); 4630 DO_SIGNAL_CHECK(SIGXFSZ); 4631 #if defined(PPC64) 4632 DO_SIGNAL_CHECK(SIGTRAP); 4633 #endif 4634 4635 // ReduceSignalUsage allows the user to override these handlers 4636 // see comments at the very top and jvm_solaris.h 4637 if (!ReduceSignalUsage) { 4638 DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL); 4639 DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL); 4640 DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL); 4641 DO_SIGNAL_CHECK(BREAK_SIGNAL); 4642 } 4643 4644 DO_SIGNAL_CHECK(SR_signum); 4645 } 4646 4647 typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *); 4648 4649 static os_sigaction_t os_sigaction = NULL; 4650 4651 void os::Linux::check_signal_handler(int sig) { 4652 char buf[O_BUFLEN]; 4653 address jvmHandler = NULL; 4654 4655 4656 struct sigaction act; 4657 if (os_sigaction == NULL) { 4658 // only trust the default sigaction, in case it has been interposed 4659 os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction"); 4660 if (os_sigaction == NULL) return; 4661 } 4662 4663 os_sigaction(sig, (struct sigaction*)NULL, &act); 4664 4665 4666 act.sa_flags &= SIGNIFICANT_SIGNAL_MASK; 4667 4668 address thisHandler = (act.sa_flags & SA_SIGINFO) 4669 ? CAST_FROM_FN_PTR(address, act.sa_sigaction) 4670 : CAST_FROM_FN_PTR(address, act.sa_handler); 4671 4672 4673 switch (sig) { 4674 case SIGSEGV: 4675 case SIGBUS: 4676 case SIGFPE: 4677 case SIGPIPE: 4678 case SIGILL: 4679 case SIGXFSZ: 4680 jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler); 4681 break; 4682 4683 case SHUTDOWN1_SIGNAL: 4684 case SHUTDOWN2_SIGNAL: 4685 case SHUTDOWN3_SIGNAL: 4686 case BREAK_SIGNAL: 4687 jvmHandler = (address)user_handler(); 4688 break; 4689 4690 default: 4691 if (sig == SR_signum) { 4692 jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler); 4693 } else { 4694 return; 4695 } 4696 break; 4697 } 4698 4699 if (thisHandler != jvmHandler) { 4700 tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN)); 4701 tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN)); 4702 tty->print_cr(" found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN)); 4703 // No need to check this sig any longer 4704 sigaddset(&check_signal_done, sig); 4705 // Running under non-interactive shell, SHUTDOWN2_SIGNAL will be reassigned SIG_IGN 4706 if (sig == SHUTDOWN2_SIGNAL && !isatty(fileno(stdin))) { 4707 tty->print_cr("Running in non-interactive shell, %s handler is replaced by shell", 4708 exception_name(sig, buf, O_BUFLEN)); 4709 } 4710 } else if(os::Linux::get_our_sigflags(sig) != 0 && (int)act.sa_flags != os::Linux::get_our_sigflags(sig)) { 4711 tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN)); 4712 tty->print("expected:"); 4713 os::Posix::print_sa_flags(tty, os::Linux::get_our_sigflags(sig)); 4714 tty->cr(); 4715 tty->print(" found:"); 4716 os::Posix::print_sa_flags(tty, act.sa_flags); 4717 tty->cr(); 4718 // No need to check this sig any longer 4719 sigaddset(&check_signal_done, sig); 4720 } 4721 4722 // Dump all the signal 4723 if (sigismember(&check_signal_done, sig)) { 4724 print_signal_handlers(tty, buf, O_BUFLEN); 4725 } 4726 } 4727 4728 extern void report_error(char* file_name, int line_no, char* title, 4729 char* format, ...); 4730 4731 // this is called _before_ the most of global arguments have been parsed 4732 void os::init(void) { 4733 char dummy; // used to get a guess on initial stack address 4734 // first_hrtime = gethrtime(); 4735 4736 clock_tics_per_sec = sysconf(_SC_CLK_TCK); 4737 4738 init_random(1234567); 4739 4740 ThreadCritical::initialize(); 4741 4742 Linux::set_page_size(sysconf(_SC_PAGESIZE)); 4743 if (Linux::page_size() == -1) { 4744 fatal("os_linux.cpp: os::init: sysconf failed (%s)", 4745 os::strerror(errno)); 4746 } 4747 init_page_sizes((size_t) Linux::page_size()); 4748 4749 Linux::initialize_system_info(); 4750 4751 Linux::initialize_os_info(); 4752 4753 // main_thread points to the aboriginal thread 4754 Linux::_main_thread = pthread_self(); 4755 4756 Linux::clock_init(); 4757 initial_time_count = javaTimeNanos(); 4758 4759 // pthread_condattr initialization for monotonic clock 4760 int status; 4761 pthread_condattr_t* _condattr = os::Linux::condAttr(); 4762 if ((status = pthread_condattr_init(_condattr)) != 0) { 4763 fatal("pthread_condattr_init: %s", os::strerror(status)); 4764 } 4765 // Only set the clock if CLOCK_MONOTONIC is available 4766 if (os::supports_monotonic_clock()) { 4767 if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) { 4768 if (status == EINVAL) { 4769 warning("Unable to use monotonic clock with relative timed-waits" \ 4770 " - changes to the time-of-day clock may have adverse affects"); 4771 } else { 4772 fatal("pthread_condattr_setclock: %s", os::strerror(status)); 4773 } 4774 } 4775 } 4776 // else it defaults to CLOCK_REALTIME 4777 4778 // retrieve entry point for pthread_setname_np 4779 Linux::_pthread_setname_np = 4780 (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np"); 4781 4782 } 4783 4784 // To install functions for atexit system call 4785 extern "C" { 4786 static void perfMemory_exit_helper() { 4787 perfMemory_exit(); 4788 } 4789 } 4790 4791 // this is called _after_ the global arguments have been parsed 4792 jint os::init_2(void) { 4793 Linux::fast_thread_clock_init(); 4794 4795 // Allocate a single page and mark it as readable for safepoint polling 4796 address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 4797 guarantee(polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page"); 4798 4799 os::set_polling_page(polling_page); 4800 log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page)); 4801 4802 if (!UseMembar) { 4803 address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 4804 guarantee(mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page"); 4805 os::set_memory_serialize_page(mem_serialize_page); 4806 log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page)); 4807 } 4808 4809 // initialize suspend/resume support - must do this before signal_sets_init() 4810 if (SR_initialize() != 0) { 4811 perror("SR_initialize failed"); 4812 return JNI_ERR; 4813 } 4814 4815 Linux::signal_sets_init(); 4816 Linux::install_signal_handlers(); 4817 4818 // Check and sets minimum stack sizes against command line options 4819 if (Posix::set_minimum_stack_sizes() == JNI_ERR) { 4820 return JNI_ERR; 4821 } 4822 Linux::capture_initial_stack(JavaThread::stack_size_at_create()); 4823 4824 #if defined(IA32) 4825 workaround_expand_exec_shield_cs_limit(); 4826 #endif 4827 4828 Linux::libpthread_init(); 4829 log_info(os)("HotSpot is running with %s, %s", 4830 Linux::glibc_version(), Linux::libpthread_version()); 4831 4832 if (UseNUMA) { 4833 if (!Linux::libnuma_init()) { 4834 UseNUMA = false; 4835 } else { 4836 if ((Linux::numa_max_node() < 1)) { 4837 // There's only one node(they start from 0), disable NUMA. 4838 UseNUMA = false; 4839 } 4840 } 4841 // With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way 4842 // we can make the adaptive lgrp chunk resizing work. If the user specified 4843 // both UseNUMA and UseLargePages (or UseSHM/UseHugeTLBFS) on the command line - warn and 4844 // disable adaptive resizing. 4845 if (UseNUMA && UseLargePages && !can_commit_large_page_memory()) { 4846 if (FLAG_IS_DEFAULT(UseNUMA)) { 4847 UseNUMA = false; 4848 } else { 4849 if (FLAG_IS_DEFAULT(UseLargePages) && 4850 FLAG_IS_DEFAULT(UseSHM) && 4851 FLAG_IS_DEFAULT(UseHugeTLBFS)) { 4852 UseLargePages = false; 4853 } else if (UseAdaptiveSizePolicy || UseAdaptiveNUMAChunkSizing) { 4854 warning("UseNUMA is not fully compatible with SHM/HugeTLBFS large pages, disabling adaptive resizing (-XX:-UseAdaptiveSizePolicy -XX:-UseAdaptiveNUMAChunkSizing)"); 4855 UseAdaptiveSizePolicy = false; 4856 UseAdaptiveNUMAChunkSizing = false; 4857 } 4858 } 4859 } 4860 if (!UseNUMA && ForceNUMA) { 4861 UseNUMA = true; 4862 } 4863 } 4864 4865 if (MaxFDLimit) { 4866 // set the number of file descriptors to max. print out error 4867 // if getrlimit/setrlimit fails but continue regardless. 4868 struct rlimit nbr_files; 4869 int status = getrlimit(RLIMIT_NOFILE, &nbr_files); 4870 if (status != 0) { 4871 log_info(os)("os::init_2 getrlimit failed: %s", os::strerror(errno)); 4872 } else { 4873 nbr_files.rlim_cur = nbr_files.rlim_max; 4874 status = setrlimit(RLIMIT_NOFILE, &nbr_files); 4875 if (status != 0) { 4876 log_info(os)("os::init_2 setrlimit failed: %s", os::strerror(errno)); 4877 } 4878 } 4879 } 4880 4881 // Initialize lock used to serialize thread creation (see os::create_thread) 4882 Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false)); 4883 4884 // at-exit methods are called in the reverse order of their registration. 4885 // atexit functions are called on return from main or as a result of a 4886 // call to exit(3C). There can be only 32 of these functions registered 4887 // and atexit() does not set errno. 4888 4889 if (PerfAllowAtExitRegistration) { 4890 // only register atexit functions if PerfAllowAtExitRegistration is set. 4891 // atexit functions can be delayed until process exit time, which 4892 // can be problematic for embedded VM situations. Embedded VMs should 4893 // call DestroyJavaVM() to assure that VM resources are released. 4894 4895 // note: perfMemory_exit_helper atexit function may be removed in 4896 // the future if the appropriate cleanup code can be added to the 4897 // VM_Exit VMOperation's doit method. 4898 if (atexit(perfMemory_exit_helper) != 0) { 4899 warning("os::init_2 atexit(perfMemory_exit_helper) failed"); 4900 } 4901 } 4902 4903 // initialize thread priority policy 4904 prio_init(); 4905 4906 return JNI_OK; 4907 } 4908 4909 // Mark the polling page as unreadable 4910 void os::make_polling_page_unreadable(void) { 4911 if (!guard_memory((char*)_polling_page, Linux::page_size())) { 4912 fatal("Could not disable polling page"); 4913 } 4914 } 4915 4916 // Mark the polling page as readable 4917 void os::make_polling_page_readable(void) { 4918 if (!linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) { 4919 fatal("Could not enable polling page"); 4920 } 4921 } 4922 4923 // older glibc versions don't have this macro (which expands to 4924 // an optimized bit-counting function) so we have to roll our own 4925 #ifndef CPU_COUNT 4926 4927 static int _cpu_count(const cpu_set_t* cpus) { 4928 int count = 0; 4929 // only look up to the number of configured processors 4930 for (int i = 0; i < os::processor_count(); i++) { 4931 if (CPU_ISSET(i, cpus)) { 4932 count++; 4933 } 4934 } 4935 return count; 4936 } 4937 4938 #define CPU_COUNT(cpus) _cpu_count(cpus) 4939 4940 #endif // CPU_COUNT 4941 4942 // Get the current number of available processors for this process. 4943 // This value can change at any time during a process's lifetime. 4944 // sched_getaffinity gives an accurate answer as it accounts for cpusets. 4945 // If it appears there may be more than 1024 processors then we do a 4946 // dynamic check - see 6515172 for details. 4947 // If anything goes wrong we fallback to returning the number of online 4948 // processors - which can be greater than the number available to the process. 4949 int os::active_processor_count() { 4950 cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors 4951 cpu_set_t* cpus_p = &cpus; 4952 int cpus_size = sizeof(cpu_set_t); 4953 4954 int configured_cpus = processor_count(); // upper bound on available cpus 4955 int cpu_count = 0; 4956 4957 // old build platforms may not support dynamic cpu sets 4958 #ifdef CPU_ALLOC 4959 4960 // To enable easy testing of the dynamic path on different platforms we 4961 // introduce a diagnostic flag: UseCpuAllocPath 4962 if (configured_cpus >= CPU_SETSIZE || UseCpuAllocPath) { 4963 // kernel may use a mask bigger than cpu_set_t 4964 log_trace(os)("active_processor_count: using dynamic path %s" 4965 "- configured processors: %d", 4966 UseCpuAllocPath ? "(forced) " : "", 4967 configured_cpus); 4968 cpus_p = CPU_ALLOC(configured_cpus); 4969 if (cpus_p != NULL) { 4970 cpus_size = CPU_ALLOC_SIZE(configured_cpus); 4971 // zero it just to be safe 4972 CPU_ZERO_S(cpus_size, cpus_p); 4973 } 4974 else { 4975 // failed to allocate so fallback to online cpus 4976 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); 4977 log_trace(os)("active_processor_count: " 4978 "CPU_ALLOC failed (%s) - using " 4979 "online processor count: %d", 4980 os::strerror(errno), online_cpus); 4981 return online_cpus; 4982 } 4983 } 4984 else { 4985 log_trace(os)("active_processor_count: using static path - configured processors: %d", 4986 configured_cpus); 4987 } 4988 #else // CPU_ALLOC 4989 // these stubs won't be executed 4990 #define CPU_COUNT_S(size, cpus) -1 4991 #define CPU_FREE(cpus) 4992 4993 log_trace(os)("active_processor_count: only static path available - configured processors: %d", 4994 configured_cpus); 4995 #endif // CPU_ALLOC 4996 4997 // pid 0 means the current thread - which we have to assume represents the process 4998 if (sched_getaffinity(0, cpus_size, cpus_p) == 0) { 4999 if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used 5000 cpu_count = CPU_COUNT_S(cpus_size, cpus_p); 5001 } 5002 else { 5003 cpu_count = CPU_COUNT(cpus_p); 5004 } 5005 log_trace(os)("active_processor_count: sched_getaffinity processor count: %d", cpu_count); 5006 } 5007 else { 5008 cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN); 5009 warning("sched_getaffinity failed (%s)- using online processor count (%d) " 5010 "which may exceed available processors", os::strerror(errno), cpu_count); 5011 } 5012 5013 if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used 5014 CPU_FREE(cpus_p); 5015 } 5016 5017 assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check"); 5018 return cpu_count; 5019 } 5020 5021 void os::set_native_thread_name(const char *name) { 5022 if (Linux::_pthread_setname_np) { 5023 char buf [16]; // according to glibc manpage, 16 chars incl. '/0' 5024 snprintf(buf, sizeof(buf), "%s", name); 5025 buf[sizeof(buf) - 1] = '\0'; 5026 const int rc = Linux::_pthread_setname_np(pthread_self(), buf); 5027 // ERANGE should not happen; all other errors should just be ignored. 5028 assert(rc != ERANGE, "pthread_setname_np failed"); 5029 } 5030 } 5031 5032 bool os::distribute_processes(uint length, uint* distribution) { 5033 // Not yet implemented. 5034 return false; 5035 } 5036 5037 bool os::bind_to_processor(uint processor_id) { 5038 // Not yet implemented. 5039 return false; 5040 } 5041 5042 /// 5043 5044 void os::SuspendedThreadTask::internal_do_task() { 5045 if (do_suspend(_thread->osthread())) { 5046 SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext()); 5047 do_task(context); 5048 do_resume(_thread->osthread()); 5049 } 5050 } 5051 5052 class PcFetcher : public os::SuspendedThreadTask { 5053 public: 5054 PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {} 5055 ExtendedPC result(); 5056 protected: 5057 void do_task(const os::SuspendedThreadTaskContext& context); 5058 private: 5059 ExtendedPC _epc; 5060 }; 5061 5062 ExtendedPC PcFetcher::result() { 5063 guarantee(is_done(), "task is not done yet."); 5064 return _epc; 5065 } 5066 5067 void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) { 5068 Thread* thread = context.thread(); 5069 OSThread* osthread = thread->osthread(); 5070 if (osthread->ucontext() != NULL) { 5071 _epc = os::Linux::ucontext_get_pc((const ucontext_t *) context.ucontext()); 5072 } else { 5073 // NULL context is unexpected, double-check this is the VMThread 5074 guarantee(thread->is_VM_thread(), "can only be called for VMThread"); 5075 } 5076 } 5077 5078 // Suspends the target using the signal mechanism and then grabs the PC before 5079 // resuming the target. Used by the flat-profiler only 5080 ExtendedPC os::get_thread_pc(Thread* thread) { 5081 // Make sure that it is called by the watcher for the VMThread 5082 assert(Thread::current()->is_Watcher_thread(), "Must be watcher"); 5083 assert(thread->is_VM_thread(), "Can only be called for VMThread"); 5084 5085 PcFetcher fetcher(thread); 5086 fetcher.run(); 5087 return fetcher.result(); 5088 } 5089 5090 //////////////////////////////////////////////////////////////////////////////// 5091 // debug support 5092 5093 bool os::find(address addr, outputStream* st) { 5094 Dl_info dlinfo; 5095 memset(&dlinfo, 0, sizeof(dlinfo)); 5096 if (dladdr(addr, &dlinfo) != 0) { 5097 st->print(PTR_FORMAT ": ", p2i(addr)); 5098 if (dlinfo.dli_sname != NULL && dlinfo.dli_saddr != NULL) { 5099 st->print("%s+" PTR_FORMAT, dlinfo.dli_sname, 5100 p2i(addr) - p2i(dlinfo.dli_saddr)); 5101 } else if (dlinfo.dli_fbase != NULL) { 5102 st->print("<offset " PTR_FORMAT ">", p2i(addr) - p2i(dlinfo.dli_fbase)); 5103 } else { 5104 st->print("<absolute address>"); 5105 } 5106 if (dlinfo.dli_fname != NULL) { 5107 st->print(" in %s", dlinfo.dli_fname); 5108 } 5109 if (dlinfo.dli_fbase != NULL) { 5110 st->print(" at " PTR_FORMAT, p2i(dlinfo.dli_fbase)); 5111 } 5112 st->cr(); 5113 5114 if (Verbose) { 5115 // decode some bytes around the PC 5116 address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size()); 5117 address end = clamp_address_in_page(addr+40, addr, os::vm_page_size()); 5118 address lowest = (address) dlinfo.dli_sname; 5119 if (!lowest) lowest = (address) dlinfo.dli_fbase; 5120 if (begin < lowest) begin = lowest; 5121 Dl_info dlinfo2; 5122 if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr 5123 && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin) { 5124 end = (address) dlinfo2.dli_saddr; 5125 } 5126 Disassembler::decode(begin, end, st); 5127 } 5128 return true; 5129 } 5130 return false; 5131 } 5132 5133 //////////////////////////////////////////////////////////////////////////////// 5134 // misc 5135 5136 // This does not do anything on Linux. This is basically a hook for being 5137 // able to use structured exception handling (thread-local exception filters) 5138 // on, e.g., Win32. 5139 void 5140 os::os_exception_wrapper(java_call_t f, JavaValue* value, const methodHandle& method, 5141 JavaCallArguments* args, Thread* thread) { 5142 f(value, method, args, thread); 5143 } 5144 5145 void os::print_statistics() { 5146 } 5147 5148 bool os::message_box(const char* title, const char* message) { 5149 int i; 5150 fdStream err(defaultStream::error_fd()); 5151 for (i = 0; i < 78; i++) err.print_raw("="); 5152 err.cr(); 5153 err.print_raw_cr(title); 5154 for (i = 0; i < 78; i++) err.print_raw("-"); 5155 err.cr(); 5156 err.print_raw_cr(message); 5157 for (i = 0; i < 78; i++) err.print_raw("="); 5158 err.cr(); 5159 5160 char buf[16]; 5161 // Prevent process from exiting upon "read error" without consuming all CPU 5162 while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); } 5163 5164 return buf[0] == 'y' || buf[0] == 'Y'; 5165 } 5166 5167 int os::stat(const char *path, struct stat *sbuf) { 5168 char pathbuf[MAX_PATH]; 5169 if (strlen(path) > MAX_PATH - 1) { 5170 errno = ENAMETOOLONG; 5171 return -1; 5172 } 5173 os::native_path(strcpy(pathbuf, path)); 5174 return ::stat(pathbuf, sbuf); 5175 } 5176 5177 // Is a (classpath) directory empty? 5178 bool os::dir_is_empty(const char* path) { 5179 DIR *dir = NULL; 5180 struct dirent *ptr; 5181 5182 dir = opendir(path); 5183 if (dir == NULL) return true; 5184 5185 // Scan the directory 5186 bool result = true; 5187 char buf[sizeof(struct dirent) + MAX_PATH]; 5188 while (result && (ptr = ::readdir(dir)) != NULL) { 5189 if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { 5190 result = false; 5191 } 5192 } 5193 closedir(dir); 5194 return result; 5195 } 5196 5197 // This code originates from JDK's sysOpen and open64_w 5198 // from src/solaris/hpi/src/system_md.c 5199 5200 int os::open(const char *path, int oflag, int mode) { 5201 if (strlen(path) > MAX_PATH - 1) { 5202 errno = ENAMETOOLONG; 5203 return -1; 5204 } 5205 5206 // All file descriptors that are opened in the Java process and not 5207 // specifically destined for a subprocess should have the close-on-exec 5208 // flag set. If we don't set it, then careless 3rd party native code 5209 // might fork and exec without closing all appropriate file descriptors 5210 // (e.g. as we do in closeDescriptors in UNIXProcess.c), and this in 5211 // turn might: 5212 // 5213 // - cause end-of-file to fail to be detected on some file 5214 // descriptors, resulting in mysterious hangs, or 5215 // 5216 // - might cause an fopen in the subprocess to fail on a system 5217 // suffering from bug 1085341. 5218 // 5219 // (Yes, the default setting of the close-on-exec flag is a Unix 5220 // design flaw) 5221 // 5222 // See: 5223 // 1085341: 32-bit stdio routines should support file descriptors >255 5224 // 4843136: (process) pipe file descriptor from Runtime.exec not being closed 5225 // 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9 5226 // 5227 // Modern Linux kernels (after 2.6.23 2007) support O_CLOEXEC with open(). 5228 // O_CLOEXEC is preferable to using FD_CLOEXEC on an open file descriptor 5229 // because it saves a system call and removes a small window where the flag 5230 // is unset. On ancient Linux kernels the O_CLOEXEC flag will be ignored 5231 // and we fall back to using FD_CLOEXEC (see below). 5232 #ifdef O_CLOEXEC 5233 oflag |= O_CLOEXEC; 5234 #endif 5235 5236 int fd = ::open64(path, oflag, mode); 5237 if (fd == -1) return -1; 5238 5239 //If the open succeeded, the file might still be a directory 5240 { 5241 struct stat64 buf64; 5242 int ret = ::fstat64(fd, &buf64); 5243 int st_mode = buf64.st_mode; 5244 5245 if (ret != -1) { 5246 if ((st_mode & S_IFMT) == S_IFDIR) { 5247 errno = EISDIR; 5248 ::close(fd); 5249 return -1; 5250 } 5251 } else { 5252 ::close(fd); 5253 return -1; 5254 } 5255 } 5256 5257 #ifdef FD_CLOEXEC 5258 // Validate that the use of the O_CLOEXEC flag on open above worked. 5259 // With recent kernels, we will perform this check exactly once. 5260 static sig_atomic_t O_CLOEXEC_is_known_to_work = 0; 5261 if (!O_CLOEXEC_is_known_to_work) { 5262 int flags = ::fcntl(fd, F_GETFD); 5263 if (flags != -1) { 5264 if ((flags & FD_CLOEXEC) != 0) 5265 O_CLOEXEC_is_known_to_work = 1; 5266 else 5267 ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC); 5268 } 5269 } 5270 #endif 5271 5272 return fd; 5273 } 5274 5275 5276 // create binary file, rewriting existing file if required 5277 int os::create_binary_file(const char* path, bool rewrite_existing) { 5278 int oflags = O_WRONLY | O_CREAT; 5279 if (!rewrite_existing) { 5280 oflags |= O_EXCL; 5281 } 5282 return ::open64(path, oflags, S_IREAD | S_IWRITE); 5283 } 5284 5285 // return current position of file pointer 5286 jlong os::current_file_offset(int fd) { 5287 return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR); 5288 } 5289 5290 // move file pointer to the specified offset 5291 jlong os::seek_to_file_offset(int fd, jlong offset) { 5292 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET); 5293 } 5294 5295 // This code originates from JDK's sysAvailable 5296 // from src/solaris/hpi/src/native_threads/src/sys_api_td.c 5297 5298 int os::available(int fd, jlong *bytes) { 5299 jlong cur, end; 5300 int mode; 5301 struct stat64 buf64; 5302 5303 if (::fstat64(fd, &buf64) >= 0) { 5304 mode = buf64.st_mode; 5305 if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) { 5306 int n; 5307 if (::ioctl(fd, FIONREAD, &n) >= 0) { 5308 *bytes = n; 5309 return 1; 5310 } 5311 } 5312 } 5313 if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) { 5314 return 0; 5315 } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) { 5316 return 0; 5317 } else if (::lseek64(fd, cur, SEEK_SET) == -1) { 5318 return 0; 5319 } 5320 *bytes = end - cur; 5321 return 1; 5322 } 5323 5324 // Map a block of memory. 5325 char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset, 5326 char *addr, size_t bytes, bool read_only, 5327 bool allow_exec) { 5328 int prot; 5329 int flags = MAP_PRIVATE; 5330 5331 if (read_only) { 5332 prot = PROT_READ; 5333 } else { 5334 prot = PROT_READ | PROT_WRITE; 5335 } 5336 5337 if (allow_exec) { 5338 prot |= PROT_EXEC; 5339 } 5340 5341 if (addr != NULL) { 5342 flags |= MAP_FIXED; 5343 } 5344 5345 char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags, 5346 fd, file_offset); 5347 if (mapped_address == MAP_FAILED) { 5348 return NULL; 5349 } 5350 return mapped_address; 5351 } 5352 5353 5354 // Remap a block of memory. 5355 char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset, 5356 char *addr, size_t bytes, bool read_only, 5357 bool allow_exec) { 5358 // same as map_memory() on this OS 5359 return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only, 5360 allow_exec); 5361 } 5362 5363 5364 // Unmap a block of memory. 5365 bool os::pd_unmap_memory(char* addr, size_t bytes) { 5366 return munmap(addr, bytes) == 0; 5367 } 5368 5369 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time); 5370 5371 static clockid_t thread_cpu_clockid(Thread* thread) { 5372 pthread_t tid = thread->osthread()->pthread_id(); 5373 clockid_t clockid; 5374 5375 // Get thread clockid 5376 int rc = os::Linux::pthread_getcpuclockid(tid, &clockid); 5377 assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code"); 5378 return clockid; 5379 } 5380 5381 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool) 5382 // are used by JVM M&M and JVMTI to get user+sys or user CPU time 5383 // of a thread. 5384 // 5385 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns 5386 // the fast estimate available on the platform. 5387 5388 jlong os::current_thread_cpu_time() { 5389 if (os::Linux::supports_fast_thread_cpu_time()) { 5390 return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID); 5391 } else { 5392 // return user + sys since the cost is the same 5393 return slow_thread_cpu_time(Thread::current(), true /* user + sys */); 5394 } 5395 } 5396 5397 jlong os::thread_cpu_time(Thread* thread) { 5398 // consistent with what current_thread_cpu_time() returns 5399 if (os::Linux::supports_fast_thread_cpu_time()) { 5400 return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread)); 5401 } else { 5402 return slow_thread_cpu_time(thread, true /* user + sys */); 5403 } 5404 } 5405 5406 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) { 5407 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) { 5408 return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID); 5409 } else { 5410 return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time); 5411 } 5412 } 5413 5414 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) { 5415 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) { 5416 return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread)); 5417 } else { 5418 return slow_thread_cpu_time(thread, user_sys_cpu_time); 5419 } 5420 } 5421 5422 // -1 on error. 5423 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) { 5424 pid_t tid = thread->osthread()->thread_id(); 5425 char *s; 5426 char stat[2048]; 5427 int statlen; 5428 char proc_name[64]; 5429 int count; 5430 long sys_time, user_time; 5431 char cdummy; 5432 int idummy; 5433 long ldummy; 5434 FILE *fp; 5435 5436 snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid); 5437 fp = fopen(proc_name, "r"); 5438 if (fp == NULL) return -1; 5439 statlen = fread(stat, 1, 2047, fp); 5440 stat[statlen] = '\0'; 5441 fclose(fp); 5442 5443 // Skip pid and the command string. Note that we could be dealing with 5444 // weird command names, e.g. user could decide to rename java launcher 5445 // to "java 1.4.2 :)", then the stat file would look like 5446 // 1234 (java 1.4.2 :)) R ... ... 5447 // We don't really need to know the command string, just find the last 5448 // occurrence of ")" and then start parsing from there. See bug 4726580. 5449 s = strrchr(stat, ')'); 5450 if (s == NULL) return -1; 5451 5452 // Skip blank chars 5453 do { s++; } while (s && isspace(*s)); 5454 5455 count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu", 5456 &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy, 5457 &ldummy, &ldummy, &ldummy, &ldummy, &ldummy, 5458 &user_time, &sys_time); 5459 if (count != 13) return -1; 5460 if (user_sys_cpu_time) { 5461 return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec); 5462 } else { 5463 return (jlong)user_time * (1000000000 / clock_tics_per_sec); 5464 } 5465 } 5466 5467 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) { 5468 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits 5469 info_ptr->may_skip_backward = false; // elapsed time not wall time 5470 info_ptr->may_skip_forward = false; // elapsed time not wall time 5471 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned 5472 } 5473 5474 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) { 5475 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits 5476 info_ptr->may_skip_backward = false; // elapsed time not wall time 5477 info_ptr->may_skip_forward = false; // elapsed time not wall time 5478 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned 5479 } 5480 5481 bool os::is_thread_cpu_time_supported() { 5482 return true; 5483 } 5484 5485 // System loadavg support. Returns -1 if load average cannot be obtained. 5486 // Linux doesn't yet have a (official) notion of processor sets, 5487 // so just return the system wide load average. 5488 int os::loadavg(double loadavg[], int nelem) { 5489 return ::getloadavg(loadavg, nelem); 5490 } 5491 5492 void os::pause() { 5493 char filename[MAX_PATH]; 5494 if (PauseAtStartupFile && PauseAtStartupFile[0]) { 5495 jio_snprintf(filename, MAX_PATH, "%s", PauseAtStartupFile); 5496 } else { 5497 jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id()); 5498 } 5499 5500 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 5501 if (fd != -1) { 5502 struct stat buf; 5503 ::close(fd); 5504 while (::stat(filename, &buf) == 0) { 5505 (void)::poll(NULL, 0, 100); 5506 } 5507 } else { 5508 jio_fprintf(stderr, 5509 "Could not open pause file '%s', continuing immediately.\n", filename); 5510 } 5511 } 5512 5513 5514 // Refer to the comments in os_solaris.cpp park-unpark. The next two 5515 // comment paragraphs are worth repeating here: 5516 // 5517 // Assumption: 5518 // Only one parker can exist on an event, which is why we allocate 5519 // them per-thread. Multiple unparkers can coexist. 5520 // 5521 // _Event serves as a restricted-range semaphore. 5522 // -1 : thread is blocked, i.e. there is a waiter 5523 // 0 : neutral: thread is running or ready, 5524 // could have been signaled after a wait started 5525 // 1 : signaled - thread is running or ready 5526 // 5527 5528 // utility to compute the abstime argument to timedwait: 5529 // millis is the relative timeout time 5530 // abstime will be the absolute timeout time 5531 // TODO: replace compute_abstime() with unpackTime() 5532 5533 static struct timespec* compute_abstime(timespec* abstime, jlong millis) { 5534 if (millis < 0) millis = 0; 5535 5536 jlong seconds = millis / 1000; 5537 millis %= 1000; 5538 if (seconds > 50000000) { // see man cond_timedwait(3T) 5539 seconds = 50000000; 5540 } 5541 5542 if (os::supports_monotonic_clock()) { 5543 struct timespec now; 5544 int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now); 5545 assert_status(status == 0, status, "clock_gettime"); 5546 abstime->tv_sec = now.tv_sec + seconds; 5547 long nanos = now.tv_nsec + millis * NANOSECS_PER_MILLISEC; 5548 if (nanos >= NANOSECS_PER_SEC) { 5549 abstime->tv_sec += 1; 5550 nanos -= NANOSECS_PER_SEC; 5551 } 5552 abstime->tv_nsec = nanos; 5553 } else { 5554 struct timeval now; 5555 int status = gettimeofday(&now, NULL); 5556 assert(status == 0, "gettimeofday"); 5557 abstime->tv_sec = now.tv_sec + seconds; 5558 long usec = now.tv_usec + millis * 1000; 5559 if (usec >= 1000000) { 5560 abstime->tv_sec += 1; 5561 usec -= 1000000; 5562 } 5563 abstime->tv_nsec = usec * 1000; 5564 } 5565 return abstime; 5566 } 5567 5568 void os::PlatformEvent::park() { // AKA "down()" 5569 // Transitions for _Event: 5570 // -1 => -1 : illegal 5571 // 1 => 0 : pass - return immediately 5572 // 0 => -1 : block; then set _Event to 0 before returning 5573 5574 // Invariant: Only the thread associated with the Event/PlatformEvent 5575 // may call park(). 5576 // TODO: assert that _Assoc != NULL or _Assoc == Self 5577 assert(_nParked == 0, "invariant"); 5578 5579 int v; 5580 for (;;) { 5581 v = _Event; 5582 if (Atomic::cmpxchg(v-1, &_Event, v) == v) break; 5583 } 5584 guarantee(v >= 0, "invariant"); 5585 if (v == 0) { 5586 // Do this the hard way by blocking ... 5587 int status = pthread_mutex_lock(_mutex); 5588 assert_status(status == 0, status, "mutex_lock"); 5589 guarantee(_nParked == 0, "invariant"); 5590 ++_nParked; 5591 while (_Event < 0) { 5592 status = pthread_cond_wait(_cond, _mutex); 5593 // for some reason, under 2.7 lwp_cond_wait() may return ETIME ... 5594 // Treat this the same as if the wait was interrupted 5595 if (status == ETIME) { status = EINTR; } 5596 assert_status(status == 0 || status == EINTR, status, "cond_wait"); 5597 } 5598 --_nParked; 5599 5600 _Event = 0; 5601 status = pthread_mutex_unlock(_mutex); 5602 assert_status(status == 0, status, "mutex_unlock"); 5603 // Paranoia to ensure our locked and lock-free paths interact 5604 // correctly with each other. 5605 OrderAccess::fence(); 5606 } 5607 guarantee(_Event >= 0, "invariant"); 5608 } 5609 5610 int os::PlatformEvent::park(jlong millis) { 5611 // Transitions for _Event: 5612 // -1 => -1 : illegal 5613 // 1 => 0 : pass - return immediately 5614 // 0 => -1 : block; then set _Event to 0 before returning 5615 5616 guarantee(_nParked == 0, "invariant"); 5617 5618 int v; 5619 for (;;) { 5620 v = _Event; 5621 if (Atomic::cmpxchg(v-1, &_Event, v) == v) break; 5622 } 5623 guarantee(v >= 0, "invariant"); 5624 if (v != 0) return OS_OK; 5625 5626 // We do this the hard way, by blocking the thread. 5627 // Consider enforcing a minimum timeout value. 5628 struct timespec abst; 5629 compute_abstime(&abst, millis); 5630 5631 int ret = OS_TIMEOUT; 5632 int status = pthread_mutex_lock(_mutex); 5633 assert_status(status == 0, status, "mutex_lock"); 5634 guarantee(_nParked == 0, "invariant"); 5635 ++_nParked; 5636 5637 // Object.wait(timo) will return because of 5638 // (a) notification 5639 // (b) timeout 5640 // (c) thread.interrupt 5641 // 5642 // Thread.interrupt and object.notify{All} both call Event::set. 5643 // That is, we treat thread.interrupt as a special case of notification. 5644 // We ignore spurious OS wakeups unless FilterSpuriousWakeups is false. 5645 // We assume all ETIME returns are valid. 5646 // 5647 // TODO: properly differentiate simultaneous notify+interrupt. 5648 // In that case, we should propagate the notify to another waiter. 5649 5650 while (_Event < 0) { 5651 status = pthread_cond_timedwait(_cond, _mutex, &abst); 5652 assert_status(status == 0 || status == EINTR || 5653 status == ETIME || status == ETIMEDOUT, 5654 status, "cond_timedwait"); 5655 if (!FilterSpuriousWakeups) break; // previous semantics 5656 if (status == ETIME || status == ETIMEDOUT) break; 5657 // We consume and ignore EINTR and spurious wakeups. 5658 } 5659 --_nParked; 5660 if (_Event >= 0) { 5661 ret = OS_OK; 5662 } 5663 _Event = 0; 5664 status = pthread_mutex_unlock(_mutex); 5665 assert_status(status == 0, status, "mutex_unlock"); 5666 assert(_nParked == 0, "invariant"); 5667 // Paranoia to ensure our locked and lock-free paths interact 5668 // correctly with each other. 5669 OrderAccess::fence(); 5670 return ret; 5671 } 5672 5673 void os::PlatformEvent::unpark() { 5674 // Transitions for _Event: 5675 // 0 => 1 : just return 5676 // 1 => 1 : just return 5677 // -1 => either 0 or 1; must signal target thread 5678 // That is, we can safely transition _Event from -1 to either 5679 // 0 or 1. 5680 // See also: "Semaphores in Plan 9" by Mullender & Cox 5681 // 5682 // Note: Forcing a transition from "-1" to "1" on an unpark() means 5683 // that it will take two back-to-back park() calls for the owning 5684 // thread to block. This has the benefit of forcing a spurious return 5685 // from the first park() call after an unpark() call which will help 5686 // shake out uses of park() and unpark() without condition variables. 5687 5688 if (Atomic::xchg(1, &_Event) >= 0) return; 5689 5690 // Wait for the thread associated with the event to vacate 5691 int status = pthread_mutex_lock(_mutex); 5692 assert_status(status == 0, status, "mutex_lock"); 5693 int AnyWaiters = _nParked; 5694 assert(AnyWaiters == 0 || AnyWaiters == 1, "invariant"); 5695 status = pthread_mutex_unlock(_mutex); 5696 assert_status(status == 0, status, "mutex_unlock"); 5697 if (AnyWaiters != 0) { 5698 // Note that we signal() *after* dropping the lock for "immortal" Events. 5699 // This is safe and avoids a common class of futile wakeups. In rare 5700 // circumstances this can cause a thread to return prematurely from 5701 // cond_{timed}wait() but the spurious wakeup is benign and the victim 5702 // will simply re-test the condition and re-park itself. 5703 // This provides particular benefit if the underlying platform does not 5704 // provide wait morphing. 5705 status = pthread_cond_signal(_cond); 5706 assert_status(status == 0, status, "cond_signal"); 5707 } 5708 } 5709 5710 5711 // JSR166 5712 // ------------------------------------------------------- 5713 5714 // The solaris and linux implementations of park/unpark are fairly 5715 // conservative for now, but can be improved. They currently use a 5716 // mutex/condvar pair, plus a a count. 5717 // Park decrements count if > 0, else does a condvar wait. Unpark 5718 // sets count to 1 and signals condvar. Only one thread ever waits 5719 // on the condvar. Contention seen when trying to park implies that someone 5720 // is unparking you, so don't wait. And spurious returns are fine, so there 5721 // is no need to track notifications. 5722 5723 // This code is common to linux and solaris and will be moved to a 5724 // common place in dolphin. 5725 // 5726 // The passed in time value is either a relative time in nanoseconds 5727 // or an absolute time in milliseconds. Either way it has to be unpacked 5728 // into suitable seconds and nanoseconds components and stored in the 5729 // given timespec structure. 5730 // Given time is a 64-bit value and the time_t used in the timespec is only 5731 // a signed-32-bit value (except on 64-bit Linux) we have to watch for 5732 // overflow if times way in the future are given. Further on Solaris versions 5733 // prior to 10 there is a restriction (see cond_timedwait) that the specified 5734 // number of seconds, in abstime, is less than current_time + 100,000,000. 5735 // As it will be 28 years before "now + 100000000" will overflow we can 5736 // ignore overflow and just impose a hard-limit on seconds using the value 5737 // of "now + 100,000,000". This places a limit on the timeout of about 3.17 5738 // years from "now". 5739 5740 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) { 5741 assert(time > 0, "convertTime"); 5742 time_t max_secs = 0; 5743 5744 if (!os::supports_monotonic_clock() || isAbsolute) { 5745 struct timeval now; 5746 int status = gettimeofday(&now, NULL); 5747 assert(status == 0, "gettimeofday"); 5748 5749 max_secs = now.tv_sec + MAX_SECS; 5750 5751 if (isAbsolute) { 5752 jlong secs = time / 1000; 5753 if (secs > max_secs) { 5754 absTime->tv_sec = max_secs; 5755 } else { 5756 absTime->tv_sec = secs; 5757 } 5758 absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC; 5759 } else { 5760 jlong secs = time / NANOSECS_PER_SEC; 5761 if (secs >= MAX_SECS) { 5762 absTime->tv_sec = max_secs; 5763 absTime->tv_nsec = 0; 5764 } else { 5765 absTime->tv_sec = now.tv_sec + secs; 5766 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000; 5767 if (absTime->tv_nsec >= NANOSECS_PER_SEC) { 5768 absTime->tv_nsec -= NANOSECS_PER_SEC; 5769 ++absTime->tv_sec; // note: this must be <= max_secs 5770 } 5771 } 5772 } 5773 } else { 5774 // must be relative using monotonic clock 5775 struct timespec now; 5776 int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now); 5777 assert_status(status == 0, status, "clock_gettime"); 5778 max_secs = now.tv_sec + MAX_SECS; 5779 jlong secs = time / NANOSECS_PER_SEC; 5780 if (secs >= MAX_SECS) { 5781 absTime->tv_sec = max_secs; 5782 absTime->tv_nsec = 0; 5783 } else { 5784 absTime->tv_sec = now.tv_sec + secs; 5785 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_nsec; 5786 if (absTime->tv_nsec >= NANOSECS_PER_SEC) { 5787 absTime->tv_nsec -= NANOSECS_PER_SEC; 5788 ++absTime->tv_sec; // note: this must be <= max_secs 5789 } 5790 } 5791 } 5792 assert(absTime->tv_sec >= 0, "tv_sec < 0"); 5793 assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs"); 5794 assert(absTime->tv_nsec >= 0, "tv_nsec < 0"); 5795 assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec"); 5796 } 5797 5798 void Parker::park(bool isAbsolute, jlong time) { 5799 // Ideally we'd do something useful while spinning, such 5800 // as calling unpackTime(). 5801 5802 // Optional fast-path check: 5803 // Return immediately if a permit is available. 5804 // We depend on Atomic::xchg() having full barrier semantics 5805 // since we are doing a lock-free update to _counter. 5806 if (Atomic::xchg(0, &_counter) > 0) return; 5807 5808 Thread* thread = Thread::current(); 5809 assert(thread->is_Java_thread(), "Must be JavaThread"); 5810 JavaThread *jt = (JavaThread *)thread; 5811 5812 // Optional optimization -- avoid state transitions if there's an interrupt pending. 5813 // Check interrupt before trying to wait 5814 if (Thread::is_interrupted(thread, false)) { 5815 return; 5816 } 5817 5818 // Next, demultiplex/decode time arguments 5819 timespec absTime; 5820 if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all 5821 return; 5822 } 5823 if (time > 0) { 5824 unpackTime(&absTime, isAbsolute, time); 5825 } 5826 5827 5828 // Enter safepoint region 5829 // Beware of deadlocks such as 6317397. 5830 // The per-thread Parker:: mutex is a classic leaf-lock. 5831 // In particular a thread must never block on the Threads_lock while 5832 // holding the Parker:: mutex. If safepoints are pending both the 5833 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 5834 ThreadBlockInVM tbivm(jt); 5835 5836 // Don't wait if cannot get lock since interference arises from 5837 // unblocking. Also. check interrupt before trying wait 5838 if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) { 5839 return; 5840 } 5841 5842 int status; 5843 if (_counter > 0) { // no wait needed 5844 _counter = 0; 5845 status = pthread_mutex_unlock(_mutex); 5846 assert_status(status == 0, status, "invariant"); 5847 // Paranoia to ensure our locked and lock-free paths interact 5848 // correctly with each other and Java-level accesses. 5849 OrderAccess::fence(); 5850 return; 5851 } 5852 5853 #ifdef ASSERT 5854 // Don't catch signals while blocked; let the running threads have the signals. 5855 // (This allows a debugger to break into the running thread.) 5856 sigset_t oldsigs; 5857 sigemptyset(&oldsigs); 5858 sigset_t* allowdebug_blocked = os::Linux::allowdebug_blocked_signals(); 5859 pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs); 5860 #endif 5861 5862 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 5863 jt->set_suspend_equivalent(); 5864 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 5865 5866 assert(_cur_index == -1, "invariant"); 5867 if (time == 0) { 5868 _cur_index = REL_INDEX; // arbitrary choice when not timed 5869 status = pthread_cond_wait(&_cond[_cur_index], _mutex); 5870 } else { 5871 _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX; 5872 status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime); 5873 } 5874 _cur_index = -1; 5875 assert_status(status == 0 || status == EINTR || 5876 status == ETIME || status == ETIMEDOUT, 5877 status, "cond_timedwait"); 5878 5879 #ifdef ASSERT 5880 pthread_sigmask(SIG_SETMASK, &oldsigs, NULL); 5881 #endif 5882 5883 _counter = 0; 5884 status = pthread_mutex_unlock(_mutex); 5885 assert_status(status == 0, status, "invariant"); 5886 // Paranoia to ensure our locked and lock-free paths interact 5887 // correctly with each other and Java-level accesses. 5888 OrderAccess::fence(); 5889 5890 // If externally suspended while waiting, re-suspend 5891 if (jt->handle_special_suspend_equivalent_condition()) { 5892 jt->java_suspend_self(); 5893 } 5894 } 5895 5896 void Parker::unpark() { 5897 int status = pthread_mutex_lock(_mutex); 5898 assert_status(status == 0, status, "invariant"); 5899 const int s = _counter; 5900 _counter = 1; 5901 // must capture correct index before unlocking 5902 int index = _cur_index; 5903 status = pthread_mutex_unlock(_mutex); 5904 assert_status(status == 0, status, "invariant"); 5905 if (s < 1 && index != -1) { 5906 // thread is definitely parked 5907 status = pthread_cond_signal(&_cond[index]); 5908 assert_status(status == 0, status, "invariant"); 5909 } 5910 } 5911 5912 5913 extern char** environ; 5914 5915 // Run the specified command in a separate process. Return its exit value, 5916 // or -1 on failure (e.g. can't fork a new process). 5917 // Unlike system(), this function can be called from signal handler. It 5918 // doesn't block SIGINT et al. 5919 int os::fork_and_exec(char* cmd) { 5920 const char * argv[4] = {"sh", "-c", cmd, NULL}; 5921 5922 pid_t pid = fork(); 5923 5924 if (pid < 0) { 5925 // fork failed 5926 return -1; 5927 5928 } else if (pid == 0) { 5929 // child process 5930 5931 execve("/bin/sh", (char* const*)argv, environ); 5932 5933 // execve failed 5934 _exit(-1); 5935 5936 } else { 5937 // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't 5938 // care about the actual exit code, for now. 5939 5940 int status; 5941 5942 // Wait for the child process to exit. This returns immediately if 5943 // the child has already exited. */ 5944 while (waitpid(pid, &status, 0) < 0) { 5945 switch (errno) { 5946 case ECHILD: return 0; 5947 case EINTR: break; 5948 default: return -1; 5949 } 5950 } 5951 5952 if (WIFEXITED(status)) { 5953 // The child exited normally; get its exit code. 5954 return WEXITSTATUS(status); 5955 } else if (WIFSIGNALED(status)) { 5956 // The child exited because of a signal 5957 // The best value to return is 0x80 + signal number, 5958 // because that is what all Unix shells do, and because 5959 // it allows callers to distinguish between process exit and 5960 // process death by signal. 5961 return 0x80 + WTERMSIG(status); 5962 } else { 5963 // Unknown exit code; pass it through 5964 return status; 5965 } 5966 } 5967 } 5968 5969 // is_headless_jre() 5970 // 5971 // Test for the existence of xawt/libmawt.so or libawt_xawt.so 5972 // in order to report if we are running in a headless jre 5973 // 5974 // Since JDK8 xawt/libmawt.so was moved into the same directory 5975 // as libawt.so, and renamed libawt_xawt.so 5976 // 5977 bool os::is_headless_jre() { 5978 struct stat statbuf; 5979 char buf[MAXPATHLEN]; 5980 char libmawtpath[MAXPATHLEN]; 5981 const char *xawtstr = "/xawt/libmawt.so"; 5982 const char *new_xawtstr = "/libawt_xawt.so"; 5983 char *p; 5984 5985 // Get path to libjvm.so 5986 os::jvm_path(buf, sizeof(buf)); 5987 5988 // Get rid of libjvm.so 5989 p = strrchr(buf, '/'); 5990 if (p == NULL) { 5991 return false; 5992 } else { 5993 *p = '\0'; 5994 } 5995 5996 // Get rid of client or server 5997 p = strrchr(buf, '/'); 5998 if (p == NULL) { 5999 return false; 6000 } else { 6001 *p = '\0'; 6002 } 6003 6004 // check xawt/libmawt.so 6005 strcpy(libmawtpath, buf); 6006 strcat(libmawtpath, xawtstr); 6007 if (::stat(libmawtpath, &statbuf) == 0) return false; 6008 6009 // check libawt_xawt.so 6010 strcpy(libmawtpath, buf); 6011 strcat(libmawtpath, new_xawtstr); 6012 if (::stat(libmawtpath, &statbuf) == 0) return false; 6013 6014 return true; 6015 } 6016 6017 // Get the default path to the core file 6018 // Returns the length of the string 6019 int os::get_core_path(char* buffer, size_t bufferSize) { 6020 /* 6021 * Max length of /proc/sys/kernel/core_pattern is 128 characters. 6022 * See https://www.kernel.org/doc/Documentation/sysctl/kernel.txt 6023 */ 6024 const int core_pattern_len = 129; 6025 char core_pattern[core_pattern_len] = {0}; 6026 6027 int core_pattern_file = ::open("/proc/sys/kernel/core_pattern", O_RDONLY); 6028 if (core_pattern_file == -1) { 6029 return -1; 6030 } 6031 6032 ssize_t ret = ::read(core_pattern_file, core_pattern, core_pattern_len); 6033 ::close(core_pattern_file); 6034 if (ret <= 0 || ret >= core_pattern_len || core_pattern[0] == '\n') { 6035 return -1; 6036 } 6037 if (core_pattern[ret-1] == '\n') { 6038 core_pattern[ret-1] = '\0'; 6039 } else { 6040 core_pattern[ret] = '\0'; 6041 } 6042 6043 char *pid_pos = strstr(core_pattern, "%p"); 6044 int written; 6045 6046 if (core_pattern[0] == '/') { 6047 written = jio_snprintf(buffer, bufferSize, "%s", core_pattern); 6048 } else { 6049 char cwd[PATH_MAX]; 6050 6051 const char* p = get_current_directory(cwd, PATH_MAX); 6052 if (p == NULL) { 6053 return -1; 6054 } 6055 6056 if (core_pattern[0] == '|') { 6057 written = jio_snprintf(buffer, bufferSize, 6058 "\"%s\" (or dumping to %s/core.%d)", 6059 &core_pattern[1], p, current_process_id()); 6060 } else { 6061 written = jio_snprintf(buffer, bufferSize, "%s/%s", p, core_pattern); 6062 } 6063 } 6064 6065 if (written < 0) { 6066 return -1; 6067 } 6068 6069 if (((size_t)written < bufferSize) && (pid_pos == NULL) && (core_pattern[0] != '|')) { 6070 int core_uses_pid_file = ::open("/proc/sys/kernel/core_uses_pid", O_RDONLY); 6071 6072 if (core_uses_pid_file != -1) { 6073 char core_uses_pid = 0; 6074 ssize_t ret = ::read(core_uses_pid_file, &core_uses_pid, 1); 6075 ::close(core_uses_pid_file); 6076 6077 if (core_uses_pid == '1') { 6078 jio_snprintf(buffer + written, bufferSize - written, 6079 ".%d", current_process_id()); 6080 } 6081 } 6082 } 6083 6084 return strlen(buffer); 6085 } 6086 6087 bool os::start_debugging(char *buf, int buflen) { 6088 int len = (int)strlen(buf); 6089 char *p = &buf[len]; 6090 6091 jio_snprintf(p, buflen-len, 6092 "\n\n" 6093 "Do you want to debug the problem?\n\n" 6094 "To debug, run 'gdb /proc/%d/exe %d'; then switch to thread " UINTX_FORMAT " (" INTPTR_FORMAT ")\n" 6095 "Enter 'yes' to launch gdb automatically (PATH must include gdb)\n" 6096 "Otherwise, press RETURN to abort...", 6097 os::current_process_id(), os::current_process_id(), 6098 os::current_thread_id(), os::current_thread_id()); 6099 6100 bool yes = os::message_box("Unexpected Error", buf); 6101 6102 if (yes) { 6103 // yes, user asked VM to launch debugger 6104 jio_snprintf(buf, sizeof(char)*buflen, "gdb /proc/%d/exe %d", 6105 os::current_process_id(), os::current_process_id()); 6106 6107 os::fork_and_exec(buf); 6108 yes = false; 6109 } 6110 return yes; 6111 } 6112 6113 6114 // Java/Compiler thread: 6115 // 6116 // Low memory addresses 6117 // P0 +------------------------+ 6118 // | |\ Java thread created by VM does not have glibc 6119 // | glibc guard page | - guard page, attached Java thread usually has 6120 // | |/ 1 glibc guard page. 6121 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size() 6122 // | |\ 6123 // | HotSpot Guard Pages | - red, yellow and reserved pages 6124 // | |/ 6125 // +------------------------+ JavaThread::stack_reserved_zone_base() 6126 // | |\ 6127 // | Normal Stack | - 6128 // | |/ 6129 // P2 +------------------------+ Thread::stack_base() 6130 // 6131 // Non-Java thread: 6132 // 6133 // Low memory addresses 6134 // P0 +------------------------+ 6135 // | |\ 6136 // | glibc guard page | - usually 1 page 6137 // | |/ 6138 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size() 6139 // | |\ 6140 // | Normal Stack | - 6141 // | |/ 6142 // P2 +------------------------+ Thread::stack_base() 6143 // 6144 // ** P1 (aka bottom) and size (P2 = P1 - size) are the address and stack size 6145 // returned from pthread_attr_getstack(). 6146 // ** Due to NPTL implementation error, linux takes the glibc guard page out 6147 // of the stack size given in pthread_attr. We work around this for 6148 // threads created by the VM. (We adapt bottom to be P1 and size accordingly.) 6149 // 6150 #ifndef ZERO 6151 static void current_stack_region(address * bottom, size_t * size) { 6152 if (os::Linux::is_initial_thread()) { 6153 // initial thread needs special handling because pthread_getattr_np() 6154 // may return bogus value. 6155 *bottom = os::Linux::initial_thread_stack_bottom(); 6156 *size = os::Linux::initial_thread_stack_size(); 6157 } else { 6158 pthread_attr_t attr; 6159 6160 int rslt = pthread_getattr_np(pthread_self(), &attr); 6161 6162 // JVM needs to know exact stack location, abort if it fails 6163 if (rslt != 0) { 6164 if (rslt == ENOMEM) { 6165 vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np"); 6166 } else { 6167 fatal("pthread_getattr_np failed with error = %d", rslt); 6168 } 6169 } 6170 6171 if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) { 6172 fatal("Cannot locate current stack attributes!"); 6173 } 6174 6175 // Work around NPTL stack guard error. 6176 size_t guard_size = 0; 6177 rslt = pthread_attr_getguardsize(&attr, &guard_size); 6178 if (rslt != 0) { 6179 fatal("pthread_attr_getguardsize failed with error = %d", rslt); 6180 } 6181 *bottom += guard_size; 6182 *size -= guard_size; 6183 6184 pthread_attr_destroy(&attr); 6185 6186 } 6187 assert(os::current_stack_pointer() >= *bottom && 6188 os::current_stack_pointer() < *bottom + *size, "just checking"); 6189 } 6190 6191 address os::current_stack_base() { 6192 address bottom; 6193 size_t size; 6194 current_stack_region(&bottom, &size); 6195 return (bottom + size); 6196 } 6197 6198 size_t os::current_stack_size() { 6199 // This stack size includes the usable stack and HotSpot guard pages 6200 // (for the threads that have Hotspot guard pages). 6201 address bottom; 6202 size_t size; 6203 current_stack_region(&bottom, &size); 6204 return size; 6205 } 6206 #endif 6207 6208 static inline struct timespec get_mtime(const char* filename) { 6209 struct stat st; 6210 int ret = os::stat(filename, &st); 6211 assert(ret == 0, "failed to stat() file '%s': %s", filename, strerror(errno)); 6212 return st.st_mtim; 6213 } 6214 6215 int os::compare_file_modified_times(const char* file1, const char* file2) { 6216 struct timespec filetime1 = get_mtime(file1); 6217 struct timespec filetime2 = get_mtime(file2); 6218 int diff = filetime1.tv_sec - filetime2.tv_sec; 6219 if (diff == 0) { 6220 return filetime1.tv_nsec - filetime2.tv_nsec; 6221 } 6222 return diff; 6223 } 6224 6225 /////////////// Unit tests /////////////// 6226 6227 #ifndef PRODUCT 6228 6229 #define test_log(...) \ 6230 do { \ 6231 if (VerboseInternalVMTests) { \ 6232 tty->print_cr(__VA_ARGS__); \ 6233 tty->flush(); \ 6234 } \ 6235 } while (false) 6236 6237 class TestReserveMemorySpecial : AllStatic { 6238 public: 6239 static void small_page_write(void* addr, size_t size) { 6240 size_t page_size = os::vm_page_size(); 6241 6242 char* end = (char*)addr + size; 6243 for (char* p = (char*)addr; p < end; p += page_size) { 6244 *p = 1; 6245 } 6246 } 6247 6248 static void test_reserve_memory_special_huge_tlbfs_only(size_t size) { 6249 if (!UseHugeTLBFS) { 6250 return; 6251 } 6252 6253 test_log("test_reserve_memory_special_huge_tlbfs_only(" SIZE_FORMAT ")", size); 6254 6255 char* addr = os::Linux::reserve_memory_special_huge_tlbfs_only(size, NULL, false); 6256 6257 if (addr != NULL) { 6258 small_page_write(addr, size); 6259 6260 os::Linux::release_memory_special_huge_tlbfs(addr, size); 6261 } 6262 } 6263 6264 static void test_reserve_memory_special_huge_tlbfs_only() { 6265 if (!UseHugeTLBFS) { 6266 return; 6267 } 6268 6269 size_t lp = os::large_page_size(); 6270 6271 for (size_t size = lp; size <= lp * 10; size += lp) { 6272 test_reserve_memory_special_huge_tlbfs_only(size); 6273 } 6274 } 6275 6276 static void test_reserve_memory_special_huge_tlbfs_mixed() { 6277 size_t lp = os::large_page_size(); 6278 size_t ag = os::vm_allocation_granularity(); 6279 6280 // sizes to test 6281 const size_t sizes[] = { 6282 lp, lp + ag, lp + lp / 2, lp * 2, 6283 lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2, 6284 lp * 10, lp * 10 + lp / 2 6285 }; 6286 const int num_sizes = sizeof(sizes) / sizeof(size_t); 6287 6288 // For each size/alignment combination, we test three scenarios: 6289 // 1) with req_addr == NULL 6290 // 2) with a non-null req_addr at which we expect to successfully allocate 6291 // 3) with a non-null req_addr which contains a pre-existing mapping, at which we 6292 // expect the allocation to either fail or to ignore req_addr 6293 6294 // Pre-allocate two areas; they shall be as large as the largest allocation 6295 // and aligned to the largest alignment we will be testing. 6296 const size_t mapping_size = sizes[num_sizes - 1] * 2; 6297 char* const mapping1 = (char*) ::mmap(NULL, mapping_size, 6298 PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, 6299 -1, 0); 6300 assert(mapping1 != MAP_FAILED, "should work"); 6301 6302 char* const mapping2 = (char*) ::mmap(NULL, mapping_size, 6303 PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, 6304 -1, 0); 6305 assert(mapping2 != MAP_FAILED, "should work"); 6306 6307 // Unmap the first mapping, but leave the second mapping intact: the first 6308 // mapping will serve as a value for a "good" req_addr (case 2). The second 6309 // mapping, still intact, as "bad" req_addr (case 3). 6310 ::munmap(mapping1, mapping_size); 6311 6312 // Case 1 6313 test_log("%s, req_addr NULL:", __FUNCTION__); 6314 test_log("size align result"); 6315 6316 for (int i = 0; i < num_sizes; i++) { 6317 const size_t size = sizes[i]; 6318 for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) { 6319 char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, NULL, false); 6320 test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " -> " PTR_FORMAT " %s", 6321 size, alignment, p2i(p), (p != NULL ? "" : "(failed)")); 6322 if (p != NULL) { 6323 assert(is_ptr_aligned(p, alignment), "must be"); 6324 small_page_write(p, size); 6325 os::Linux::release_memory_special_huge_tlbfs(p, size); 6326 } 6327 } 6328 } 6329 6330 // Case 2 6331 test_log("%s, req_addr non-NULL:", __FUNCTION__); 6332 test_log("size align req_addr result"); 6333 6334 for (int i = 0; i < num_sizes; i++) { 6335 const size_t size = sizes[i]; 6336 for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) { 6337 char* const req_addr = (char*) align_ptr_up(mapping1, alignment); 6338 char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false); 6339 test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " -> " PTR_FORMAT " %s", 6340 size, alignment, p2i(req_addr), p2i(p), 6341 ((p != NULL ? (p == req_addr ? "(exact match)" : "") : "(failed)"))); 6342 if (p != NULL) { 6343 assert(p == req_addr, "must be"); 6344 small_page_write(p, size); 6345 os::Linux::release_memory_special_huge_tlbfs(p, size); 6346 } 6347 } 6348 } 6349 6350 // Case 3 6351 test_log("%s, req_addr non-NULL with preexisting mapping:", __FUNCTION__); 6352 test_log("size align req_addr result"); 6353 6354 for (int i = 0; i < num_sizes; i++) { 6355 const size_t size = sizes[i]; 6356 for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) { 6357 char* const req_addr = (char*) align_ptr_up(mapping2, alignment); 6358 char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false); 6359 test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " -> " PTR_FORMAT " %s", 6360 size, alignment, p2i(req_addr), p2i(p), ((p != NULL ? "" : "(failed)"))); 6361 // as the area around req_addr contains already existing mappings, the API should always 6362 // return NULL (as per contract, it cannot return another address) 6363 assert(p == NULL, "must be"); 6364 } 6365 } 6366 6367 ::munmap(mapping2, mapping_size); 6368 6369 } 6370 6371 static void test_reserve_memory_special_huge_tlbfs() { 6372 if (!UseHugeTLBFS) { 6373 return; 6374 } 6375 6376 test_reserve_memory_special_huge_tlbfs_only(); 6377 test_reserve_memory_special_huge_tlbfs_mixed(); 6378 } 6379 6380 static void test_reserve_memory_special_shm(size_t size, size_t alignment) { 6381 if (!UseSHM) { 6382 return; 6383 } 6384 6385 test_log("test_reserve_memory_special_shm(" SIZE_FORMAT ", " SIZE_FORMAT ")", size, alignment); 6386 6387 char* addr = os::Linux::reserve_memory_special_shm(size, alignment, NULL, false); 6388 6389 if (addr != NULL) { 6390 assert(is_ptr_aligned(addr, alignment), "Check"); 6391 assert(is_ptr_aligned(addr, os::large_page_size()), "Check"); 6392 6393 small_page_write(addr, size); 6394 6395 os::Linux::release_memory_special_shm(addr, size); 6396 } 6397 } 6398 6399 static void test_reserve_memory_special_shm() { 6400 size_t lp = os::large_page_size(); 6401 size_t ag = os::vm_allocation_granularity(); 6402 6403 for (size_t size = ag; size < lp * 3; size += ag) { 6404 for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) { 6405 test_reserve_memory_special_shm(size, alignment); 6406 } 6407 } 6408 } 6409 6410 static void test() { 6411 test_reserve_memory_special_huge_tlbfs(); 6412 test_reserve_memory_special_shm(); 6413 } 6414 }; 6415 6416 void TestReserveMemorySpecial_test() { 6417 TestReserveMemorySpecial::test(); 6418 } 6419 6420 #endif