annotate src/os/linux/vm/os_linux.cpp @ 9029:1485461a0fd1

8197429: Increased stack guard causes segfaults on x86-32 Reviewed-by: dholmes
author aph
date Fri, 06 Jul 2018 17:25:06 +0100
parents 79b4c0a88c00
children 97d605522fcb
rev   line source
duke@0 1 /*
dbuck@8815 2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 // no precompiled headers
stefank@1879 26 #include "classfile/classLoader.hpp"
stefank@1879 27 #include "classfile/systemDictionary.hpp"
stefank@1879 28 #include "classfile/vmSymbols.hpp"
stefank@1879 29 #include "code/icBuffer.hpp"
stefank@1879 30 #include "code/vtableStubs.hpp"
stefank@1879 31 #include "compiler/compileBroker.hpp"
twisti@3883 32 #include "compiler/disassembler.hpp"
stefank@1879 33 #include "interpreter/interpreter.hpp"
stefank@1879 34 #include "jvm_linux.h"
stefank@1879 35 #include "memory/allocation.inline.hpp"
stefank@1879 36 #include "memory/filemap.hpp"
stefank@1879 37 #include "mutex_linux.inline.hpp"
stefank@1879 38 #include "oops/oop.inline.hpp"
stefank@1879 39 #include "os_share_linux.hpp"
poonam@8787 40 #include "osContainer_linux.hpp"
stefank@1879 41 #include "prims/jniFastGetField.hpp"
stefank@1879 42 #include "prims/jvm.h"
stefank@1879 43 #include "prims/jvm_misc.hpp"
stefank@1879 44 #include "runtime/arguments.hpp"
stefank@1879 45 #include "runtime/extendedPC.hpp"
stefank@1879 46 #include "runtime/globals.hpp"
stefank@1879 47 #include "runtime/interfaceSupport.hpp"
iklam@4275 48 #include "runtime/init.hpp"
stefank@1879 49 #include "runtime/java.hpp"
stefank@1879 50 #include "runtime/javaCalls.hpp"
stefank@1879 51 #include "runtime/mutexLocker.hpp"
stefank@1879 52 #include "runtime/objectMonitor.hpp"
goetz@6316 53 #include "runtime/orderAccess.inline.hpp"
stefank@1879 54 #include "runtime/osThread.hpp"
stefank@1879 55 #include "runtime/perfMemory.hpp"
stefank@1879 56 #include "runtime/sharedRuntime.hpp"
stefank@1879 57 #include "runtime/statSampler.hpp"
stefank@1879 58 #include "runtime/stubRoutines.hpp"
stefank@3864 59 #include "runtime/thread.inline.hpp"
stefank@1879 60 #include "runtime/threadCritical.hpp"
stefank@1879 61 #include "runtime/timer.hpp"
stefank@1879 62 #include "services/attachListener.hpp"
zgu@4276 63 #include "services/memTracker.hpp"
stefank@1879 64 #include "services/runtimeService.hpp"
zgu@1929 65 #include "utilities/decoder.hpp"
stefank@1879 66 #include "utilities/defaultStream.hpp"
stefank@1879 67 #include "utilities/events.hpp"
iklam@4275 68 #include "utilities/elfFile.hpp"
stefank@1879 69 #include "utilities/growableArray.hpp"
stefank@1879 70 #include "utilities/vmError.hpp"
duke@0 71
duke@0 72 // put OS-includes here
duke@0 73 # include <sys/types.h>
duke@0 74 # include <sys/mman.h>
bobv@1601 75 # include <sys/stat.h>
bobv@1601 76 # include <sys/select.h>
duke@0 77 # include <pthread.h>
duke@0 78 # include <signal.h>
duke@0 79 # include <errno.h>
duke@0 80 # include <dlfcn.h>
duke@0 81 # include <stdio.h>
duke@0 82 # include <unistd.h>
duke@0 83 # include <sys/resource.h>
duke@0 84 # include <pthread.h>
duke@0 85 # include <sys/stat.h>
duke@0 86 # include <sys/time.h>
duke@0 87 # include <sys/times.h>
duke@0 88 # include <sys/utsname.h>
duke@0 89 # include <sys/socket.h>
duke@0 90 # include <sys/wait.h>
duke@0 91 # include <pwd.h>
duke@0 92 # include <poll.h>
duke@0 93 # include <semaphore.h>
duke@0 94 # include <fcntl.h>
duke@0 95 # include <string.h>
duke@0 96 # include <syscall.h>
duke@0 97 # include <sys/sysinfo.h>
duke@0 98 # include <gnu/libc-version.h>
duke@0 99 # include <sys/ipc.h>
duke@0 100 # include <sys/shm.h>
duke@0 101 # include <link.h>
coleenp@1320 102 # include <stdint.h>
coleenp@1320 103 # include <inttypes.h>
dholmes@1940 104 # include <sys/ioctl.h>
duke@0 105
drchase@6245 106 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6245 107
shshahma@8233 108 #ifndef _GNU_SOURCE
shshahma@8233 109 #define _GNU_SOURCE
shshahma@8233 110 #include <sched.h>
shshahma@8233 111 #undef _GNU_SOURCE
shshahma@8233 112 #else
shshahma@8233 113 #include <sched.h>
shshahma@8233 114 #endif
shshahma@8233 115
tschatzl@4769 116 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
tschatzl@4769 117 // getrusage() is prepared to handle the associated failure.
tschatzl@4769 118 #ifndef RUSAGE_THREAD
tschatzl@4769 119 #define RUSAGE_THREAD (1) /* only the calling thread */
tschatzl@4769 120 #endif
tschatzl@4769 121
duke@0 122 #define MAX_PATH (2 * K)
duke@0 123
mgronlun@5952 124 #define MAX_SECS 100000000
mgronlun@5952 125
duke@0 126 // for timer info max values which include all bits
duke@0 127 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
duke@0 128
ctornqvi@2085 129 #define LARGEPAGES_BIT (1 << 6)
duke@0 130 ////////////////////////////////////////////////////////////////////////////////
duke@0 131 // global variables
duke@0 132 julong os::Linux::_physical_memory = 0;
duke@0 133
duke@0 134 address os::Linux::_initial_thread_stack_bottom = NULL;
duke@0 135 uintptr_t os::Linux::_initial_thread_stack_size = 0;
duke@0 136
duke@0 137 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;
duke@0 138 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
duke@0 139 Mutex* os::Linux::_createThread_lock = NULL;
duke@0 140 pthread_t os::Linux::_main_thread;
duke@0 141 int os::Linux::_page_size = -1;
vladidan@4561 142 const int os::Linux::_vm_default_page_size = (8 * K);
duke@0 143 bool os::Linux::_is_floating_stack = false;
duke@0 144 bool os::Linux::_is_NPTL = false;
duke@0 145 bool os::Linux::_supports_fast_thread_cpu_time = false;
xlu@199 146 const char * os::Linux::_glibc_version = NULL;
xlu@199 147 const char * os::Linux::_libpthread_version = NULL;
dholmes@5244 148 pthread_condattr_t os::Linux::_condattr[1];
duke@0 149
duke@0 150 static jlong initial_time_count=0;
duke@0 151
duke@0 152 static int clock_tics_per_sec = 100;
duke@0 153
duke@0 154 // For diagnostics to print a message once. see run_periodic_checks
duke@0 155 static sigset_t check_signal_done;
goetz@6006 156 static bool check_signals = true;
duke@0 157
duke@0 158 static pid_t _initial_pid = 0;
duke@0 159
duke@0 160 /* Signal number used to suspend/resume a thread */
duke@0 161
duke@0 162 /* do not use any signal number less than SIGSEGV, see 4355769 */
duke@0 163 static int SR_signum = SIGUSR2;
duke@0 164 sigset_t SR_sigset;
duke@0 165
kamg@242 166 /* Used to protect dlsym() calls */
kamg@242 167 static pthread_mutex_t dl_mutex;
kamg@242 168
sla@4802 169 // Declarations
sla@4802 170 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
sla@4802 171
duke@0 172 // utility functions
duke@0 173
duke@0 174 static int SR_initialize();
duke@0 175
duke@0 176 julong os::available_memory() {
duke@0 177 return Linux::available_memory();
duke@0 178 }
duke@0 179
duke@0 180 julong os::Linux::available_memory() {
duke@0 181 // values in struct sysinfo are "unsigned long"
duke@0 182 struct sysinfo si;
poonam@8787 183 julong avail_mem;
poonam@8787 184
poonam@8787 185 if (OSContainer::is_containerized()) {
poonam@8787 186 jlong mem_limit, mem_usage;
poonam@8787 187 if ((mem_limit = OSContainer::memory_limit_in_bytes()) < 1) {
poonam@8787 188 if (PrintContainerInfo) {
poonam@8787 189 tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",
poonam@8787 190 mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);
poonam@8787 191 }
poonam@8787 192 }
poonam@8787 193
poonam@8787 194 if (mem_limit > 0 && (mem_usage = OSContainer::memory_usage_in_bytes()) < 1) {
poonam@8787 195 if (PrintContainerInfo) {
poonam@8787 196 tty->print_cr("container memory usage failed: " JLONG_FORMAT ", using host value", mem_usage);
poonam@8787 197 }
poonam@8787 198 }
poonam@8787 199
poonam@8787 200 if (mem_limit > 0 && mem_usage > 0 ) {
poonam@8787 201 avail_mem = mem_limit > mem_usage ? (julong)mem_limit - (julong)mem_usage : 0;
poonam@8787 202 if (PrintContainerInfo) {
poonam@8787 203 tty->print_cr("available container memory: " JULONG_FORMAT, avail_mem);
poonam@8787 204 }
poonam@8787 205 return avail_mem;
poonam@8787 206 }
poonam@8787 207 }
poonam@8787 208
duke@0 209 sysinfo(&si);
poonam@8787 210 avail_mem = (julong)si.freeram * si.mem_unit;
poonam@8787 211 if (Verbose) {
poonam@8787 212 tty->print_cr("available memory: " JULONG_FORMAT, avail_mem);
poonam@8787 213 }
poonam@8787 214 return avail_mem;
duke@0 215 }
duke@0 216
duke@0 217 julong os::physical_memory() {
poonam@8787 218 jlong phys_mem = 0;
poonam@8787 219 if (OSContainer::is_containerized()) {
poonam@8787 220 jlong mem_limit;
poonam@8787 221 if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
poonam@8787 222 if (PrintContainerInfo) {
poonam@8787 223 tty->print_cr("total container memory: " JLONG_FORMAT, mem_limit);
poonam@8787 224 }
poonam@8787 225 return mem_limit;
poonam@8787 226 }
poonam@8787 227
poonam@8787 228 if (PrintContainerInfo) {
poonam@8787 229 tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",
poonam@8787 230 mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);
poonam@8787 231 }
poonam@8787 232 }
poonam@8787 233
poonam@8787 234 phys_mem = Linux::physical_memory();
poonam@8787 235 if (Verbose) {
poonam@8787 236 tty->print_cr("total system memory: " JLONG_FORMAT, phys_mem);
poonam@8787 237 }
poonam@8787 238 return phys_mem;
duke@0 239 }
duke@0 240
duke@0 241 ////////////////////////////////////////////////////////////////////////////////
duke@0 242 // environment support
duke@0 243
duke@0 244 bool os::getenv(const char* name, char* buf, int len) {
duke@0 245 const char* val = ::getenv(name);
duke@0 246 if (val != NULL && strlen(val) < (size_t)len) {
duke@0 247 strcpy(buf, val);
duke@0 248 return true;
duke@0 249 }
duke@0 250 if (len > 0) buf[0] = 0; // return a null string
duke@0 251 return false;
duke@0 252 }
duke@0 253
duke@0 254
duke@0 255 // Return true if user is running as root.
duke@0 256
duke@0 257 bool os::have_special_privileges() {
duke@0 258 static bool init = false;
duke@0 259 static bool privileges = false;
duke@0 260 if (!init) {
duke@0 261 privileges = (getuid() != geteuid()) || (getgid() != getegid());
duke@0 262 init = true;
duke@0 263 }
duke@0 264 return privileges;
duke@0 265 }
duke@0 266
duke@0 267
duke@0 268 #ifndef SYS_gettid
duke@0 269 // i386: 224, ia64: 1105, amd64: 186, sparc 143
dholmes@7342 270 #ifdef __ia64__
dholmes@7342 271 #define SYS_gettid 1105
dholmes@7342 272 #else
dholmes@7342 273 #ifdef __i386__
dholmes@7342 274 #define SYS_gettid 224
dholmes@7342 275 #else
dholmes@7342 276 #ifdef __amd64__
dholmes@7342 277 #define SYS_gettid 186
dholmes@7342 278 #else
dholmes@7342 279 #ifdef __sparc__
dholmes@7342 280 #define SYS_gettid 143
dholmes@7342 281 #else
dholmes@7342 282 #error define gettid for the arch
dholmes@7342 283 #endif
dholmes@7342 284 #endif
dholmes@7342 285 #endif
dholmes@7342 286 #endif
duke@0 287 #endif
duke@0 288
duke@0 289 // Cpu architecture string
dlong@7116 290 static char cpu_arch[] = HOTSPOT_LIB_ARCH;
duke@0 291
duke@0 292 // pid_t gettid()
duke@0 293 //
duke@0 294 // Returns the kernel thread id of the currently running thread. Kernel
duke@0 295 // thread id is used to access /proc.
duke@0 296 //
duke@0 297 // (Note that getpid() on LinuxThreads returns kernel thread id too; but
duke@0 298 // on NPTL, it returns the same pid for all threads, as required by POSIX.)
duke@0 299 //
duke@0 300 pid_t os::Linux::gettid() {
duke@0 301 int rslt = syscall(SYS_gettid);
duke@0 302 if (rslt == -1) {
duke@0 303 // old kernel, no NPTL support
duke@0 304 return getpid();
duke@0 305 } else {
duke@0 306 return (pid_t)rslt;
duke@0 307 }
duke@0 308 }
duke@0 309
duke@0 310 // Most versions of linux have a bug where the number of processors are
duke@0 311 // determined by looking at the /proc file system. In a chroot environment,
duke@0 312 // the system call returns 1. This causes the VM to act as if it is
duke@0 313 // a single processor and elide locking (see is_MP() call).
duke@0 314 static bool unsafe_chroot_detected = false;
xlu@199 315 static const char *unstable_chroot_error = "/proc file system not found.\n"
xlu@199 316 "Java may be unstable running multithreaded in a chroot "
xlu@199 317 "environment on Linux when /proc filesystem is not mounted.";
duke@0 318
duke@0 319 void os::Linux::initialize_system_info() {
phh@1123 320 set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
phh@1123 321 if (processor_count() == 1) {
duke@0 322 pid_t pid = os::Linux::gettid();
duke@0 323 char fname[32];
duke@0 324 jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
duke@0 325 FILE *fp = fopen(fname, "r");
duke@0 326 if (fp == NULL) {
duke@0 327 unsafe_chroot_detected = true;
duke@0 328 } else {
duke@0 329 fclose(fp);
duke@0 330 }
duke@0 331 }
duke@0 332 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
phh@1123 333 assert(processor_count() > 0, "linux error");
duke@0 334 }
duke@0 335
duke@0 336 void os::init_system_properties_values() {
duke@0 337 // The next steps are taken in the product version:
duke@0 338 //
dcubed@3957 339 // Obtain the JAVA_HOME value from the location of libjvm.so.
duke@0 340 // This library should be located at:
dcubed@3957 341 // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
duke@0 342 //
duke@0 343 // If "/jre/lib/" appears at the right place in the path, then we
dcubed@3957 344 // assume libjvm.so is installed in a JDK and we use this path.
duke@0 345 //
duke@0 346 // Otherwise exit with message: "Could not create the Java virtual machine."
duke@0 347 //
duke@0 348 // The following extra steps are taken in the debugging version:
duke@0 349 //
duke@0 350 // If "/jre/lib/" does NOT appear at the right place in the path
duke@0 351 // instead of exit check for $JAVA_HOME environment variable.
duke@0 352 //
duke@0 353 // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
dcubed@3957 354 // then we append a fake suffix "hotspot/libjvm.so" to this path so
dcubed@3957 355 // it looks like libjvm.so is installed there
dcubed@3957 356 // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
duke@0 357 //
duke@0 358 // Otherwise exit.
duke@0 359 //
duke@0 360 // Important note: if the location of libjvm.so changes this
duke@0 361 // code needs to be changed accordingly.
duke@0 362
goetz@6121 363 // See ld(1):
goetz@6121 364 // The linker uses the following search paths to locate required
goetz@6121 365 // shared libraries:
goetz@6121 366 // 1: ...
goetz@6121 367 // ...
goetz@6121 368 // 7: The default directories, normally /lib and /usr/lib.
kvn@509 369 #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))
kvn@509 370 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
kvn@509 371 #else
duke@0 372 #define DEFAULT_LIBPATH "/lib:/usr/lib"
kvn@509 373 #endif
duke@0 374
goetz@6121 375 // Base path of extensions installed on the system.
goetz@6121 376 #define SYS_EXT_DIR "/usr/java/packages"
duke@0 377 #define EXTENSIONS_DIR "/lib/ext"
duke@0 378 #define ENDORSED_DIR "/lib/endorsed"
goetz@6121 379
goetz@6121 380 // Buffer that fits several sprintfs.
goetz@6121 381 // Note that the space for the colon and the trailing null are provided
goetz@6121 382 // by the nulls included by the sizeof operator.
goetz@6121 383 const size_t bufsize =
goetz@6121 384 MAX3((size_t)MAXPATHLEN, // For dll_dir & friends.
goetz@6121 385 (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir
goetz@6121 386 (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir
goetz@6121 387 char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
goetz@6121 388
goetz@6121 389 // sysclasspath, java_home, dll_dir
duke@0 390 {
goetz@6121 391 char *pslash;
goetz@6121 392 os::jvm_path(buf, bufsize);
goetz@6121 393
goetz@6121 394 // Found the full path to libjvm.so.
goetz@6121 395 // Now cut the path to <java_home>/jre if we can.
goetz@6121 396 *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.
goetz@6121 397 pslash = strrchr(buf, '/');
goetz@6121 398 if (pslash != NULL) {
goetz@6121 399 *pslash = '\0'; // Get rid of /{client|server|hotspot}.
goetz@6121 400 }
goetz@6121 401 Arguments::set_dll_dir(buf);
goetz@6121 402
goetz@6121 403 if (pslash != NULL) {
goetz@6121 404 pslash = strrchr(buf, '/');
goetz@6121 405 if (pslash != NULL) {
goetz@6121 406 *pslash = '\0'; // Get rid of /<arch>.
duke@0 407 pslash = strrchr(buf, '/');
duke@0 408 if (pslash != NULL) {
goetz@6121 409 *pslash = '\0'; // Get rid of /lib.
duke@0 410 }
goetz@6121 411 }
duke@0 412 }
goetz@6121 413 Arguments::set_java_home(buf);
goetz@6121 414 set_boot_path('/', ':');
goetz@6121 415 }
goetz@6121 416
goetz@6121 417 // Where to look for native libraries.
goetz@6121 418 //
goetz@6121 419 // Note: Due to a legacy implementation, most of the library path
goetz@6121 420 // is set in the launcher. This was to accomodate linking restrictions
goetz@6121 421 // on legacy Linux implementations (which are no longer supported).
goetz@6121 422 // Eventually, all the library path setting will be done here.
goetz@6121 423 //
goetz@6121 424 // However, to prevent the proliferation of improperly built native
goetz@6121 425 // libraries, the new path component /usr/java/packages is added here.
goetz@6121 426 // Eventually, all the library path setting will be done here.
goetz@6121 427 {
goetz@6121 428 // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
goetz@6121 429 // should always exist (until the legacy problem cited above is
goetz@6121 430 // addressed).
goetz@6121 431 const char *v = ::getenv("LD_LIBRARY_PATH");
goetz@6121 432 const char *v_colon = ":";
goetz@6121 433 if (v == NULL) { v = ""; v_colon = ""; }
goetz@6121 434 // That's +1 for the colon and +1 for the trailing '\0'.
goetz@6121 435 char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
goetz@6121 436 strlen(v) + 1 +
goetz@6121 437 sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1,
goetz@6121 438 mtInternal);
goetz@6121 439 sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);
goetz@6121 440 Arguments::set_library_path(ld_library_path);
goetz@6121 441 FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
goetz@6121 442 }
goetz@6121 443
goetz@6121 444 // Extensions directories.
goetz@6121 445 sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
goetz@6121 446 Arguments::set_ext_dirs(buf);
goetz@6121 447
goetz@6121 448 // Endorsed standards default directory.
goetz@6121 449 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
goetz@6121 450 Arguments::set_endorsed_dirs(buf);
goetz@6121 451
goetz@6121 452 FREE_C_HEAP_ARRAY(char, buf, mtInternal);
goetz@6121 453
goetz@6121 454 #undef DEFAULT_LIBPATH
goetz@6121 455 #undef SYS_EXT_DIR
duke@0 456 #undef EXTENSIONS_DIR
duke@0 457 #undef ENDORSED_DIR
duke@0 458 }
duke@0 459
duke@0 460 ////////////////////////////////////////////////////////////////////////////////
duke@0 461 // breakpoint support
duke@0 462
duke@0 463 void os::breakpoint() {
duke@0 464 BREAKPOINT;
duke@0 465 }
duke@0 466
duke@0 467 extern "C" void breakpoint() {
duke@0 468 // use debugger to set breakpoint here
duke@0 469 }
duke@0 470
duke@0 471 ////////////////////////////////////////////////////////////////////////////////
duke@0 472 // signal support
duke@0 473
duke@0 474 debug_only(static bool signal_sets_initialized = false);
duke@0 475 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
duke@0 476
duke@0 477 bool os::Linux::is_sig_ignored(int sig) {
duke@0 478 struct sigaction oact;
duke@0 479 sigaction(sig, (struct sigaction*)NULL, &oact);
duke@0 480 void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction)
duke@0 481 : CAST_FROM_FN_PTR(void*, oact.sa_handler);
duke@0 482 if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN))
duke@0 483 return true;
duke@0 484 else
duke@0 485 return false;
duke@0 486 }
duke@0 487
duke@0 488 void os::Linux::signal_sets_init() {
duke@0 489 // Should also have an assertion stating we are still single-threaded.
duke@0 490 assert(!signal_sets_initialized, "Already initialized");
duke@0 491 // Fill in signals that are necessarily unblocked for all threads in
duke@0 492 // the VM. Currently, we unblock the following signals:
duke@0 493 // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden
duke@0 494 // by -Xrs (=ReduceSignalUsage));
duke@0 495 // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all
duke@0 496 // other threads. The "ReduceSignalUsage" boolean tells us not to alter
duke@0 497 // the dispositions or masks wrt these signals.
duke@0 498 // Programs embedding the VM that want to use the above signals for their
duke@0 499 // own purposes must, at this time, use the "-Xrs" option to prevent
duke@0 500 // interference with shutdown hooks and BREAK_SIGNAL thread dumping.
duke@0 501 // (See bug 4345157, and other related bugs).
duke@0 502 // In reality, though, unblocking these signals is really a nop, since
duke@0 503 // these signals are not blocked by default.
duke@0 504 sigemptyset(&unblocked_sigs);
duke@0 505 sigemptyset(&allowdebug_blocked_sigs);
duke@0 506 sigaddset(&unblocked_sigs, SIGILL);
duke@0 507 sigaddset(&unblocked_sigs, SIGSEGV);
duke@0 508 sigaddset(&unblocked_sigs, SIGBUS);
duke@0 509 sigaddset(&unblocked_sigs, SIGFPE);
goetz@6047 510 #if defined(PPC64)
goetz@6047 511 sigaddset(&unblocked_sigs, SIGTRAP);
goetz@6047 512 #endif
duke@0 513 sigaddset(&unblocked_sigs, SR_signum);
duke@0 514
duke@0 515 if (!ReduceSignalUsage) {
duke@0 516 if (!os::Linux::is_sig_ignored(SHUTDOWN1_SIGNAL)) {
duke@0 517 sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);
duke@0 518 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL);
duke@0 519 }
duke@0 520 if (!os::Linux::is_sig_ignored(SHUTDOWN2_SIGNAL)) {
duke@0 521 sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);
duke@0 522 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL);
duke@0 523 }
duke@0 524 if (!os::Linux::is_sig_ignored(SHUTDOWN3_SIGNAL)) {
duke@0 525 sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);
duke@0 526 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL);
duke@0 527 }
duke@0 528 }
duke@0 529 // Fill in signals that are blocked by all but the VM thread.
duke@0 530 sigemptyset(&vm_sigs);
duke@0 531 if (!ReduceSignalUsage)
duke@0 532 sigaddset(&vm_sigs, BREAK_SIGNAL);
duke@0 533 debug_only(signal_sets_initialized = true);
duke@0 534
duke@0 535 }
duke@0 536
duke@0 537 // These are signals that are unblocked while a thread is running Java.
duke@0 538 // (For some reason, they get blocked by default.)
duke@0 539 sigset_t* os::Linux::unblocked_signals() {
duke@0 540 assert(signal_sets_initialized, "Not initialized");
duke@0 541 return &unblocked_sigs;
duke@0 542 }
duke@0 543
duke@0 544 // These are the signals that are blocked while a (non-VM) thread is
duke@0 545 // running Java. Only the VM thread handles these signals.
duke@0 546 sigset_t* os::Linux::vm_signals() {
duke@0 547 assert(signal_sets_initialized, "Not initialized");
duke@0 548 return &vm_sigs;
duke@0 549 }
duke@0 550
duke@0 551 // These are signals that are blocked during cond_wait to allow debugger in
duke@0 552 sigset_t* os::Linux::allowdebug_blocked_signals() {
duke@0 553 assert(signal_sets_initialized, "Not initialized");
duke@0 554 return &allowdebug_blocked_sigs;
duke@0 555 }
duke@0 556
duke@0 557 void os::Linux::hotspot_sigmask(Thread* thread) {
duke@0 558
duke@0 559 //Save caller's signal mask before setting VM signal mask
duke@0 560 sigset_t caller_sigmask;
duke@0 561 pthread_sigmask(SIG_BLOCK, NULL, &caller_sigmask);
duke@0 562
duke@0 563 OSThread* osthread = thread->osthread();
duke@0 564 osthread->set_caller_sigmask(caller_sigmask);
duke@0 565
duke@0 566 pthread_sigmask(SIG_UNBLOCK, os::Linux::unblocked_signals(), NULL);
duke@0 567
duke@0 568 if (!ReduceSignalUsage) {
duke@0 569 if (thread->is_VM_thread()) {
duke@0 570 // Only the VM thread handles BREAK_SIGNAL ...
duke@0 571 pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL);
duke@0 572 } else {
duke@0 573 // ... all other threads block BREAK_SIGNAL
duke@0 574 pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);
duke@0 575 }
duke@0 576 }
duke@0 577 }
duke@0 578
duke@0 579 //////////////////////////////////////////////////////////////////////////////
duke@0 580 // detecting pthread library
duke@0 581
duke@0 582 void os::Linux::libpthread_init() {
duke@0 583 // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION
duke@0 584 // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a
duke@0 585 // generic name for earlier versions.
duke@0 586 // Define macros here so we can build HotSpot on old systems.
duke@0 587 # ifndef _CS_GNU_LIBC_VERSION
duke@0 588 # define _CS_GNU_LIBC_VERSION 2
duke@0 589 # endif
duke@0 590 # ifndef _CS_GNU_LIBPTHREAD_VERSION
duke@0 591 # define _CS_GNU_LIBPTHREAD_VERSION 3
duke@0 592 # endif
duke@0 593
duke@0 594 size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
duke@0 595 if (n > 0) {
zgu@3465 596 char *str = (char *)malloc(n, mtInternal);
duke@0 597 confstr(_CS_GNU_LIBC_VERSION, str, n);
duke@0 598 os::Linux::set_glibc_version(str);
duke@0 599 } else {
duke@0 600 // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()
duke@0 601 static char _gnu_libc_version[32];
duke@0 602 jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),
duke@0 603 "glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());
duke@0 604 os::Linux::set_glibc_version(_gnu_libc_version);
duke@0 605 }
duke@0 606
duke@0 607 n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
duke@0 608 if (n > 0) {
zgu@3465 609 char *str = (char *)malloc(n, mtInternal);
duke@0 610 confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);
duke@0 611 // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells
duke@0 612 // us "NPTL-0.29" even we are running with LinuxThreads. Check if this
xlu@199 613 // is the case. LinuxThreads has a hard limit on max number of threads.
xlu@199 614 // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.
xlu@199 615 // On the other hand, NPTL does not have such a limit, sysconf()
xlu@199 616 // will return -1 and errno is not changed. Check if it is really NPTL.
duke@0 617 if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&
xlu@199 618 strstr(str, "NPTL") &&
xlu@199 619 sysconf(_SC_THREAD_THREADS_MAX) > 0) {
xlu@199 620 free(str);
xlu@199 621 os::Linux::set_libpthread_version("linuxthreads");
xlu@199 622 } else {
xlu@199 623 os::Linux::set_libpthread_version(str);
duke@0 624 }
duke@0 625 } else {
xlu@199 626 // glibc before 2.3.2 only has LinuxThreads.
xlu@199 627 os::Linux::set_libpthread_version("linuxthreads");
duke@0 628 }
duke@0 629
duke@0 630 if (strstr(libpthread_version(), "NPTL")) {
duke@0 631 os::Linux::set_is_NPTL();
duke@0 632 } else {
duke@0 633 os::Linux::set_is_LinuxThreads();
duke@0 634 }
duke@0 635
duke@0 636 // LinuxThreads have two flavors: floating-stack mode, which allows variable
duke@0 637 // stack size; and fixed-stack mode. NPTL is always floating-stack.
duke@0 638 if (os::Linux::is_NPTL() || os::Linux::supports_variable_stack_size()) {
duke@0 639 os::Linux::set_is_floating_stack();
duke@0 640 }
duke@0 641 }
duke@0 642
duke@0 643 /////////////////////////////////////////////////////////////////////////////
duke@0 644 // thread stack
duke@0 645
duke@0 646 // Force Linux kernel to expand current thread stack. If "bottom" is close
duke@0 647 // to the stack guard, caller should block all signals.
duke@0 648 //
duke@0 649 // MAP_GROWSDOWN:
duke@0 650 // A special mmap() flag that is used to implement thread stacks. It tells
duke@0 651 // kernel that the memory region should extend downwards when needed. This
duke@0 652 // allows early versions of LinuxThreads to only mmap the first few pages
duke@0 653 // when creating a new thread. Linux kernel will automatically expand thread
duke@0 654 // stack as needed (on page faults).
duke@0 655 //
duke@0 656 // However, because the memory region of a MAP_GROWSDOWN stack can grow on
duke@0 657 // demand, if a page fault happens outside an already mapped MAP_GROWSDOWN
duke@0 658 // region, it's hard to tell if the fault is due to a legitimate stack
duke@0 659 // access or because of reading/writing non-exist memory (e.g. buffer
duke@0 660 // overrun). As a rule, if the fault happens below current stack pointer,
duke@0 661 // Linux kernel does not expand stack, instead a SIGSEGV is sent to the
duke@0 662 // application (see Linux kernel fault.c).
duke@0 663 //
duke@0 664 // This Linux feature can cause SIGSEGV when VM bangs thread stack for
duke@0 665 // stack overflow detection.
duke@0 666 //
duke@0 667 // Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do
duke@0 668 // not use this flag. However, the stack of initial thread is not created
duke@0 669 // by pthread, it is still MAP_GROWSDOWN. Also it's possible (though
duke@0 670 // unlikely) that user code can create a thread with MAP_GROWSDOWN stack
duke@0 671 // and then attach the thread to JVM.
duke@0 672 //
duke@0 673 // To get around the problem and allow stack banging on Linux, we need to
duke@0 674 // manually expand thread stack after receiving the SIGSEGV.
duke@0 675 //
duke@0 676 // There are two ways to expand thread stack to address "bottom", we used
duke@0 677 // both of them in JVM before 1.5:
duke@0 678 // 1. adjust stack pointer first so that it is below "bottom", and then
duke@0 679 // touch "bottom"
duke@0 680 // 2. mmap() the page in question
duke@0 681 //
duke@0 682 // Now alternate signal stack is gone, it's harder to use 2. For instance,
duke@0 683 // if current sp is already near the lower end of page 101, and we need to
duke@0 684 // call mmap() to map page 100, it is possible that part of the mmap() frame
duke@0 685 // will be placed in page 100. When page 100 is mapped, it is zero-filled.
duke@0 686 // That will destroy the mmap() frame and cause VM to crash.
duke@0 687 //
duke@0 688 // The following code works by adjusting sp first, then accessing the "bottom"
duke@0 689 // page to force a page fault. Linux kernel will then automatically expand the
duke@0 690 // stack mapping.
duke@0 691 //
duke@0 692 // _expand_stack_to() assumes its frame size is less than page size, which
duke@0 693 // should always be true if the function is not inlined.
duke@0 694
duke@0 695 #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute
duke@0 696 #define NOINLINE
duke@0 697 #else
duke@0 698 #define NOINLINE __attribute__ ((noinline))
duke@0 699 #endif
duke@0 700
duke@0 701 static void _expand_stack_to(address bottom) NOINLINE;
duke@0 702
duke@0 703 static void _expand_stack_to(address bottom) {
duke@0 704 address sp;
duke@0 705 size_t size;
duke@0 706 volatile char *p;
duke@0 707
duke@0 708 // Adjust bottom to point to the largest address within the same page, it
duke@0 709 // gives us a one-page buffer if alloca() allocates slightly more memory.
duke@0 710 bottom = (address)align_size_down((uintptr_t)bottom, os::Linux::page_size());
duke@0 711 bottom += os::Linux::page_size() - 1;
duke@0 712
duke@0 713 // sp might be slightly above current stack pointer; if that's the case, we
duke@0 714 // will alloca() a little more space than necessary, which is OK. Don't use
duke@0 715 // os::current_stack_pointer(), as its result can be slightly below current
duke@0 716 // stack pointer, causing us to not alloca enough to reach "bottom".
duke@0 717 sp = (address)&sp;
duke@0 718
duke@0 719 if (sp > bottom) {
duke@0 720 size = sp - bottom;
duke@0 721 p = (volatile char *)alloca(size);
duke@0 722 assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?");
duke@0 723 p[0] = '\0';
duke@0 724 }
duke@0 725 }
duke@0 726
aph@9029 727 void os::Linux::expand_stack_to(address bottom) {
aph@9029 728 _expand_stack_to(bottom);
aph@9029 729 }
aph@9029 730
duke@0 731 bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
duke@0 732 assert(t!=NULL, "just checking");
duke@0 733 assert(t->osthread()->expanding_stack(), "expand should be set");
duke@0 734 assert(t->stack_base() != NULL, "stack_base was not initialized");
duke@0 735
duke@0 736 if (addr < t->stack_base() && addr >= t->stack_yellow_zone_base()) {
duke@0 737 sigset_t mask_all, old_sigset;
duke@0 738 sigfillset(&mask_all);
duke@0 739 pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset);
duke@0 740 _expand_stack_to(addr);
duke@0 741 pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
duke@0 742 return true;
duke@0 743 }
duke@0 744 return false;
duke@0 745 }
duke@0 746
duke@0 747 //////////////////////////////////////////////////////////////////////////////
duke@0 748 // create new thread
duke@0 749
duke@0 750 static address highest_vm_reserved_address();
duke@0 751
duke@0 752 // check if it's safe to start a new thread
duke@0 753 static bool _thread_safety_check(Thread* thread) {
duke@0 754 if (os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack()) {
duke@0 755 // Fixed stack LinuxThreads (SuSE Linux/x86, and some versions of Redhat)
duke@0 756 // Heap is mmap'ed at lower end of memory space. Thread stacks are
duke@0 757 // allocated (MAP_FIXED) from high address space. Every thread stack
duke@0 758 // occupies a fixed size slot (usually 2Mbytes, but user can change
duke@0 759 // it to other values if they rebuild LinuxThreads).
duke@0 760 //
duke@0 761 // Problem with MAP_FIXED is that mmap() can still succeed even part of
duke@0 762 // the memory region has already been mmap'ed. That means if we have too
duke@0 763 // many threads and/or very large heap, eventually thread stack will
duke@0 764 // collide with heap.
duke@0 765 //
duke@0 766 // Here we try to prevent heap/stack collision by comparing current
duke@0 767 // stack bottom with the highest address that has been mmap'ed by JVM
duke@0 768 // plus a safety margin for memory maps created by native code.
duke@0 769 //
duke@0 770 // This feature can be disabled by setting ThreadSafetyMargin to 0
duke@0 771 //
duke@0 772 if (ThreadSafetyMargin > 0) {
duke@0 773 address stack_bottom = os::current_stack_base() - os::current_stack_size();
duke@0 774
duke@0 775 // not safe if our stack extends below the safety margin
duke@0 776 return stack_bottom - ThreadSafetyMargin >= highest_vm_reserved_address();
duke@0 777 } else {
duke@0 778 return true;
duke@0 779 }
duke@0 780 } else {
duke@0 781 // Floating stack LinuxThreads or NPTL:
duke@0 782 // Unlike fixed stack LinuxThreads, thread stacks are not MAP_FIXED. When
duke@0 783 // there's not enough space left, pthread_create() will fail. If we come
duke@0 784 // here, that means enough space has been reserved for stack.
duke@0 785 return true;
duke@0 786 }
duke@0 787 }
duke@0 788
duke@0 789 // Thread start routine for all newly created threads
duke@0 790 static void *java_start(Thread *thread) {
duke@0 791 // Try to randomize the cache line index of hot stack frames.
duke@0 792 // This helps when threads of the same stack traces evict each other's
duke@0 793 // cache lines. The threads can be either from the same JVM instance, or
duke@0 794 // from different JVM instances. The benefit is especially true for
duke@0 795 // processors with hyperthreading technology.
duke@0 796 static int counter = 0;
duke@0 797 int pid = os::current_process_id();
duke@0 798 alloca(((pid ^ counter++) & 7) * 128);
duke@0 799
duke@0 800 ThreadLocalStorage::set_thread(thread);
duke@0 801
duke@0 802 OSThread* osthread = thread->osthread();
duke@0 803 Monitor* sync = osthread->startThread_lock();
duke@0 804
duke@0 805 // non floating stack LinuxThreads needs extra check, see above
duke@0 806 if (!_thread_safety_check(thread)) {
duke@0 807 // notify parent thread
duke@0 808 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
duke@0 809 osthread->set_state(ZOMBIE);
duke@0 810 sync->notify_all();
duke@0 811 return NULL;
duke@0 812 }
duke@0 813
duke@0 814 // thread_id is kernel thread id (similar to Solaris LWP id)
duke@0 815 osthread->set_thread_id(os::Linux::gettid());
duke@0 816
duke@0 817 if (UseNUMA) {
duke@0 818 int lgrp_id = os::numa_get_group_id();
duke@0 819 if (lgrp_id != -1) {
duke@0 820 thread->set_lgrp_id(lgrp_id);
duke@0 821 }
duke@0 822 }
duke@0 823 // initialize signal mask for this thread
duke@0 824 os::Linux::hotspot_sigmask(thread);
duke@0 825
duke@0 826 // initialize floating point control register
duke@0 827 os::Linux::init_thread_fpu_state();
duke@0 828
duke@0 829 // handshaking with parent thread
duke@0 830 {
duke@0 831 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
duke@0 832
duke@0 833 // notify parent thread
duke@0 834 osthread->set_state(INITIALIZED);
duke@0 835 sync->notify_all();
duke@0 836
duke@0 837 // wait until os::start_thread()
duke@0 838 while (osthread->get_state() == INITIALIZED) {
duke@0 839 sync->wait(Mutex::_no_safepoint_check_flag);
duke@0 840 }
duke@0 841 }
duke@0 842
duke@0 843 // call one more level start routine
duke@0 844 thread->run();
duke@0 845
duke@0 846 return 0;
duke@0 847 }
duke@0 848
duke@0 849 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
duke@0 850 assert(thread->osthread() == NULL, "caller responsible");
duke@0 851
duke@0 852 // Allocate the OSThread object
duke@0 853 OSThread* osthread = new OSThread(NULL, NULL);
duke@0 854 if (osthread == NULL) {
duke@0 855 return false;
duke@0 856 }
duke@0 857
duke@0 858 // set the correct thread state
duke@0 859 osthread->set_thread_type(thr_type);
duke@0 860
duke@0 861 // Initial state is ALLOCATED but not INITIALIZED
duke@0 862 osthread->set_state(ALLOCATED);
duke@0 863
duke@0 864 thread->set_osthread(osthread);
duke@0 865
duke@0 866 // init thread attributes
duke@0 867 pthread_attr_t attr;
duke@0 868 pthread_attr_init(&attr);
duke@0 869 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
duke@0 870
duke@0 871 // stack size
duke@0 872 if (os::Linux::supports_variable_stack_size()) {
duke@0 873 // calculate stack size if it's not specified by caller
duke@0 874 if (stack_size == 0) {
duke@0 875 stack_size = os::Linux::default_stack_size(thr_type);
duke@0 876
duke@0 877 switch (thr_type) {
duke@0 878 case os::java_thread:
coleenp@1787 879 // Java threads use ThreadStackSize which default value can be
coleenp@1787 880 // changed with the flag -Xss
coleenp@1787 881 assert (JavaThread::stack_size_at_create() > 0, "this should be set");
coleenp@1787 882 stack_size = JavaThread::stack_size_at_create();
duke@0 883 break;
duke@0 884 case os::compiler_thread:
duke@0 885 if (CompilerThreadStackSize > 0) {
duke@0 886 stack_size = (size_t)(CompilerThreadStackSize * K);
duke@0 887 break;
duke@0 888 } // else fall through:
duke@0 889 // use VMThreadStackSize if CompilerThreadStackSize is not defined
duke@0 890 case os::vm_thread:
duke@0 891 case os::pgc_thread:
duke@0 892 case os::cgc_thread:
duke@0 893 case os::watcher_thread:
duke@0 894 if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
duke@0 895 break;
duke@0 896 }
duke@0 897 }
duke@0 898
duke@0 899 stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);
duke@0 900 pthread_attr_setstacksize(&attr, stack_size);
duke@0 901 } else {
duke@0 902 // let pthread_create() pick the default value.
duke@0 903 }
duke@0 904
duke@0 905 // glibc guard page
duke@0 906 pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
duke@0 907
duke@0 908 ThreadState state;
duke@0 909
duke@0 910 {
duke@0 911 // Serialize thread creation if we are running with fixed stack LinuxThreads
duke@0 912 bool lock = os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack();
duke@0 913 if (lock) {
duke@0 914 os::Linux::createThread_lock()->lock_without_safepoint_check();
duke@0 915 }
duke@0 916
duke@0 917 pthread_t tid;
duke@0 918 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
duke@0 919
duke@0 920 pthread_attr_destroy(&attr);
duke@0 921
duke@0 922 if (ret != 0) {
duke@0 923 if (PrintMiscellaneous && (Verbose || WizardMode)) {
duke@0 924 perror("pthread_create()");
duke@0 925 }
duke@0 926 // Need to clean up stuff we've allocated so far
duke@0 927 thread->set_osthread(NULL);
duke@0 928 delete osthread;
duke@0 929 if (lock) os::Linux::createThread_lock()->unlock();
duke@0 930 return false;
duke@0 931 }
duke@0 932
duke@0 933 // Store pthread info into the OSThread
duke@0 934 osthread->set_pthread_id(tid);
duke@0 935
duke@0 936 // Wait until child thread is either initialized or aborted
duke@0 937 {
duke@0 938 Monitor* sync_with_child = osthread->startThread_lock();
duke@0 939 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
duke@0 940 while ((state = osthread->get_state()) == ALLOCATED) {
duke@0 941 sync_with_child->wait(Mutex::_no_safepoint_check_flag);
duke@0 942 }
duke@0 943 }
duke@0 944
duke@0 945 if (lock) {
duke@0 946 os::Linux::createThread_lock()->unlock();
duke@0 947 }
duke@0 948 }
duke@0 949
duke@0 950 // Aborted due to thread limit being reached
duke@0 951 if (state == ZOMBIE) {
duke@0 952 thread->set_osthread(NULL);
duke@0 953 delete osthread;
duke@0 954 return false;
duke@0 955 }
duke@0 956
duke@0 957 // The thread is returned suspended (in state INITIALIZED),
duke@0 958 // and is started higher up in the call chain
duke@0 959 assert(state == INITIALIZED, "race condition");
duke@0 960 return true;
duke@0 961 }
duke@0 962
duke@0 963 /////////////////////////////////////////////////////////////////////////////
duke@0 964 // attach existing thread
duke@0 965
duke@0 966 // bootstrap the main thread
duke@0 967 bool os::create_main_thread(JavaThread* thread) {
duke@0 968 assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread");
duke@0 969 return create_attached_thread(thread);
duke@0 970 }
duke@0 971
duke@0 972 bool os::create_attached_thread(JavaThread* thread) {
duke@0 973 #ifdef ASSERT
duke@0 974 thread->verify_not_published();
duke@0 975 #endif
duke@0 976
duke@0 977 // Allocate the OSThread object
duke@0 978 OSThread* osthread = new OSThread(NULL, NULL);
duke@0 979
duke@0 980 if (osthread == NULL) {
duke@0 981 return false;
duke@0 982 }
duke@0 983
duke@0 984 // Store pthread info into the OSThread
duke@0 985 osthread->set_thread_id(os::Linux::gettid());
duke@0 986 osthread->set_pthread_id(::pthread_self());
duke@0 987
duke@0 988 // initialize floating point control register
duke@0 989 os::Linux::init_thread_fpu_state();
duke@0 990
duke@0 991 // Initial thread state is RUNNABLE
duke@0 992 osthread->set_state(RUNNABLE);
duke@0 993
duke@0 994 thread->set_osthread(osthread);
duke@0 995
duke@0 996 if (UseNUMA) {
duke@0 997 int lgrp_id = os::numa_get_group_id();
duke@0 998 if (lgrp_id != -1) {
duke@0 999 thread->set_lgrp_id(lgrp_id);
duke@0 1000 }
duke@0 1001 }
duke@0 1002
dbuck@8815 1003 if (os::is_primordial_thread()) {
dbuck@8815 1004 // If current thread is primordial thread, its stack is mapped on demand,
duke@0 1005 // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
duke@0 1006 // the entire stack region to avoid SEGV in stack banging.
duke@0 1007 // It is also useful to get around the heap-stack-gap problem on SuSE
duke@0 1008 // kernel (see 4821821 for details). We first expand stack to the top
duke@0 1009 // of yellow zone, then enable stack yellow zone (order is significant,
duke@0 1010 // enabling yellow zone first will crash JVM on SuSE Linux), so there
duke@0 1011 // is no gap between the last two virtual memory regions.
duke@0 1012
duke@0 1013 JavaThread *jt = (JavaThread *)thread;
duke@0 1014 address addr = jt->stack_yellow_zone_base();
duke@0 1015 assert(addr != NULL, "initialization problem?");
duke@0 1016 assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
duke@0 1017
duke@0 1018 osthread->set_expanding_stack();
duke@0 1019 os::Linux::manually_expand_stack(jt, addr);
duke@0 1020 osthread->clear_expanding_stack();
duke@0 1021 }
duke@0 1022
duke@0 1023 // initialize signal mask for this thread
duke@0 1024 // and save the caller's signal mask
duke@0 1025 os::Linux::hotspot_sigmask(thread);
duke@0 1026
duke@0 1027 return true;
duke@0 1028 }
duke@0 1029
duke@0 1030 void os::pd_start_thread(Thread* thread) {
duke@0 1031 OSThread * osthread = thread->osthread();
duke@0 1032 assert(osthread->get_state() != INITIALIZED, "just checking");
duke@0 1033 Monitor* sync_with_child = osthread->startThread_lock();
duke@0 1034 MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
duke@0 1035 sync_with_child->notify();
duke@0 1036 }
duke@0 1037
duke@0 1038 // Free Linux resources related to the OSThread
duke@0 1039 void os::free_thread(OSThread* osthread) {
duke@0 1040 assert(osthread != NULL, "osthread not set");
duke@0 1041
duke@0 1042 if (Thread::current()->osthread() == osthread) {
duke@0 1043 // Restore caller's signal mask
duke@0 1044 sigset_t sigmask = osthread->caller_sigmask();
duke@0 1045 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
duke@0 1046 }
duke@0 1047
duke@0 1048 delete osthread;
duke@0 1049 }
duke@0 1050
duke@0 1051 //////////////////////////////////////////////////////////////////////////////
duke@0 1052 // thread local storage
duke@0 1053
kevinw@6118 1054 // Restore the thread pointer if the destructor is called. This is in case
kevinw@6118 1055 // someone from JNI code sets up a destructor with pthread_key_create to run
kevinw@6118 1056 // detachCurrentThread on thread death. Unless we restore the thread pointer we
kevinw@6118 1057 // will hang or crash. When detachCurrentThread is called the key will be set
kevinw@6118 1058 // to null and we will not be called again. If detachCurrentThread is never
kevinw@6118 1059 // called we could loop forever depending on the pthread implementation.
kevinw@6118 1060 static void restore_thread_pointer(void* p) {
kevinw@6118 1061 Thread* thread = (Thread*) p;
kevinw@6118 1062 os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
kevinw@6118 1063 }
kevinw@6118 1064
duke@0 1065 int os::allocate_thread_local_storage() {
duke@0 1066 pthread_key_t key;
kevinw@6118 1067 int rslt = pthread_key_create(&key, restore_thread_pointer);
duke@0 1068 assert(rslt == 0, "cannot allocate thread local storage");
duke@0 1069 return (int)key;
duke@0 1070 }
duke@0 1071
duke@0 1072 // Note: This is currently not used by VM, as we don't destroy TLS key
duke@0 1073 // on VM exit.
duke@0 1074 void os::free_thread_local_storage(int index) {
duke@0 1075 int rslt = pthread_key_delete((pthread_key_t)index);
duke@0 1076 assert(rslt == 0, "invalid index");
duke@0 1077 }
duke@0 1078
duke@0 1079 void os::thread_local_storage_at_put(int index, void* value) {
duke@0 1080 int rslt = pthread_setspecific((pthread_key_t)index, value);
duke@0 1081 assert(rslt == 0, "pthread_setspecific failed");
duke@0 1082 }
duke@0 1083
duke@0 1084 extern "C" Thread* get_thread() {
duke@0 1085 return ThreadLocalStorage::thread();
duke@0 1086 }
duke@0 1087
duke@0 1088 //////////////////////////////////////////////////////////////////////////////
dbuck@8815 1089 // primordial thread
dbuck@8815 1090
dbuck@8815 1091 // Check if current thread is the primordial thread, similar to Solaris thr_main.
dbuck@8815 1092 bool os::is_primordial_thread(void) {
duke@0 1093 char dummy;
duke@0 1094 // If called before init complete, thread stack bottom will be null.
duke@0 1095 // Can be called if fatal error occurs before initialization.
dbuck@8815 1096 if (os::Linux::initial_thread_stack_bottom() == NULL) return false;
dbuck@8815 1097 assert(os::Linux::initial_thread_stack_bottom() != NULL &&
dbuck@8815 1098 os::Linux::initial_thread_stack_size() != 0,
dbuck@8815 1099 "os::init did not locate primordial thread's stack region");
dbuck@8815 1100 if ((address)&dummy >= os::Linux::initial_thread_stack_bottom() &&
dbuck@8815 1101 (address)&dummy < os::Linux::initial_thread_stack_bottom() +
dbuck@8815 1102 os::Linux::initial_thread_stack_size()) {
duke@0 1103 return true;
dbuck@8815 1104 } else {
dbuck@8815 1105 return false;
dbuck@8815 1106 }
duke@0 1107 }
duke@0 1108
duke@0 1109 // Find the virtual memory area that contains addr
duke@0 1110 static bool find_vma(address addr, address* vma_low, address* vma_high) {
duke@0 1111 FILE *fp = fopen("/proc/self/maps", "r");
duke@0 1112 if (fp) {
duke@0 1113 address low, high;
duke@0 1114 while (!feof(fp)) {
duke@0 1115 if (fscanf(fp, "%p-%p", &low, &high) == 2) {
duke@0 1116 if (low <= addr && addr < high) {
duke@0 1117 if (vma_low) *vma_low = low;
duke@0 1118 if (vma_high) *vma_high = high;
duke@0 1119 fclose (fp);
duke@0 1120 return true;
duke@0 1121 }
duke@0 1122 }
duke@0 1123 for (;;) {
duke@0 1124 int ch = fgetc(fp);
duke@0 1125 if (ch == EOF || ch == (int)'\n') break;
duke@0 1126 }
duke@0 1127 }
duke@0 1128 fclose(fp);
duke@0 1129 }
duke@0 1130 return false;
duke@0 1131 }
duke@0 1132
dbuck@8815 1133 // Locate primordial thread stack. This special handling of primordial thread stack
duke@0 1134 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
dholmes@8236 1135 // bogus value for the primordial process thread. While the launcher has created
dholmes@8236 1136 // the VM in a new thread since JDK 6, we still have to allow for the use of the
dholmes@8236 1137 // JNI invocation API from a primordial thread.
duke@0 1138 void os::Linux::capture_initial_stack(size_t max_size) {
dholmes@8236 1139
dholmes@8236 1140 // max_size is either 0 (which means accept OS default for thread stacks) or
dholmes@8236 1141 // a user-specified value known to be at least the minimum needed. If we
dholmes@8236 1142 // are actually on the primordial thread we can make it appear that we have a
dholmes@8236 1143 // smaller max_size stack by inserting the guard pages at that location. But we
dholmes@8236 1144 // cannot do anything to emulate a larger stack than what has been provided by
dholmes@8236 1145 // the OS or threading library. In fact if we try to use a stack greater than
dholmes@8236 1146 // what is set by rlimit then we will crash the hosting process.
dholmes@8236 1147
dholmes@8236 1148 // Maximum stack size is the easy part, get it from RLIMIT_STACK.
dholmes@8236 1149 // If this is "unlimited" then it will be a huge value.
duke@0 1150 struct rlimit rlim;
duke@0 1151 getrlimit(RLIMIT_STACK, &rlim);
dholmes@8236 1152 size_t stack_size = rlim.rlim_cur;
duke@0 1153
duke@0 1154 // 6308388: a bug in ld.so will relocate its own .data section to the
duke@0 1155 // lower end of primordial stack; reduce ulimit -s value a little bit
duke@0 1156 // so we won't install guard page on ld.so's data section.
dbuck@8815 1157 // But ensure we don't underflow the stack size - allow 1 page spare
dbuck@8815 1158 if (stack_size >= (size_t)(3 * page_size())) {
dbuck@8815 1159 stack_size -= 2 * page_size();
dbuck@8815 1160 }
duke@0 1161
duke@0 1162 // Try to figure out where the stack base (top) is. This is harder.
duke@0 1163 //
duke@0 1164 // When an application is started, glibc saves the initial stack pointer in
duke@0 1165 // a global variable "__libc_stack_end", which is then used by system
duke@0 1166 // libraries. __libc_stack_end should be pretty close to stack top. The
duke@0 1167 // variable is available since the very early days. However, because it is
duke@0 1168 // a private interface, it could disappear in the future.
duke@0 1169 //
duke@0 1170 // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar
duke@0 1171 // to __libc_stack_end, it is very close to stack top, but isn't the real
duke@0 1172 // stack top. Note that /proc may not exist if VM is running as a chroot
duke@0 1173 // program, so reading /proc/<pid>/stat could fail. Also the contents of
duke@0 1174 // /proc/<pid>/stat could change in the future (though unlikely).
duke@0 1175 //
duke@0 1176 // We try __libc_stack_end first. If that doesn't work, look for
duke@0 1177 // /proc/<pid>/stat. If neither of them works, we use current stack pointer
duke@0 1178 // as a hint, which should work well in most cases.
duke@0 1179
duke@0 1180 uintptr_t stack_start;
duke@0 1181
duke@0 1182 // try __libc_stack_end first
duke@0 1183 uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end");
duke@0 1184 if (p && *p) {
duke@0 1185 stack_start = *p;
duke@0 1186 } else {
duke@0 1187 // see if we can get the start_stack field from /proc/self/stat
duke@0 1188 FILE *fp;
duke@0 1189 int pid;
duke@0 1190 char state;
duke@0 1191 int ppid;
duke@0 1192 int pgrp;
duke@0 1193 int session;
duke@0 1194 int nr;
duke@0 1195 int tpgrp;
duke@0 1196 unsigned long flags;
duke@0 1197 unsigned long minflt;
duke@0 1198 unsigned long cminflt;
duke@0 1199 unsigned long majflt;
duke@0 1200 unsigned long cmajflt;
duke@0 1201 unsigned long utime;
duke@0 1202 unsigned long stime;
duke@0 1203 long cutime;
duke@0 1204 long cstime;
duke@0 1205 long prio;
duke@0 1206 long nice;
duke@0 1207 long junk;
duke@0 1208 long it_real;
duke@0 1209 uintptr_t start;
duke@0 1210 uintptr_t vsize;
bobv@1601 1211 intptr_t rss;
bobv@1601 1212 uintptr_t rsslim;
duke@0 1213 uintptr_t scodes;
duke@0 1214 uintptr_t ecode;
duke@0 1215 int i;
duke@0 1216
duke@0 1217 // Figure what the primordial thread stack base is. Code is inspired
duke@0 1218 // by email from Hans Boehm. /proc/self/stat begins with current pid,
duke@0 1219 // followed by command name surrounded by parentheses, state, etc.
duke@0 1220 char stat[2048];
duke@0 1221 int statlen;
duke@0 1222
duke@0 1223 fp = fopen("/proc/self/stat", "r");
duke@0 1224 if (fp) {
duke@0 1225 statlen = fread(stat, 1, 2047, fp);
duke@0 1226 stat[statlen] = '\0';
duke@0 1227 fclose(fp);
duke@0 1228
duke@0 1229 // Skip pid and the command string. Note that we could be dealing with
duke@0 1230 // weird command names, e.g. user could decide to rename java launcher
duke@0 1231 // to "java 1.4.2 :)", then the stat file would look like
duke@0 1232 // 1234 (java 1.4.2 :)) R ... ...
duke@0 1233 // We don't really need to know the command string, just find the last
duke@0 1234 // occurrence of ")" and then start parsing from there. See bug 4726580.
duke@0 1235 char * s = strrchr(stat, ')');
duke@0 1236
duke@0 1237 i = 0;
duke@0 1238 if (s) {
duke@0 1239 // Skip blank chars
duke@0 1240 do s++; while (isspace(*s));
duke@0 1241
bobv@1601 1242 #define _UFM UINTX_FORMAT
bobv@1601 1243 #define _DFM INTX_FORMAT
bobv@1601 1244
bobv@1601 1245 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */
bobv@1601 1246 /* 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 */
bobv@1601 1247 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,
duke@0 1248 &state, /* 3 %c */
duke@0 1249 &ppid, /* 4 %d */
duke@0 1250 &pgrp, /* 5 %d */
duke@0 1251 &session, /* 6 %d */
duke@0 1252 &nr, /* 7 %d */
duke@0 1253 &tpgrp, /* 8 %d */
duke@0 1254 &flags, /* 9 %lu */
duke@0 1255 &minflt, /* 10 %lu */
duke@0 1256 &cminflt, /* 11 %lu */
duke@0 1257 &majflt, /* 12 %lu */
duke@0 1258 &cmajflt, /* 13 %lu */
duke@0 1259 &utime, /* 14 %lu */
duke@0 1260 &stime, /* 15 %lu */
duke@0 1261 &cutime, /* 16 %ld */
duke@0 1262 &cstime, /* 17 %ld */
duke@0 1263 &prio, /* 18 %ld */
duke@0 1264 &nice, /* 19 %ld */
duke@0 1265 &junk, /* 20 %ld */
duke@0 1266 &it_real, /* 21 %ld */
bobv@1601 1267 &start, /* 22 UINTX_FORMAT */
bobv@1601 1268 &vsize, /* 23 UINTX_FORMAT */
bobv@1601 1269 &rss, /* 24 INTX_FORMAT */
bobv@1601 1270 &rsslim, /* 25 UINTX_FORMAT */
bobv@1601 1271 &scodes, /* 26 UINTX_FORMAT */
bobv@1601 1272 &ecode, /* 27 UINTX_FORMAT */
bobv@1601 1273 &stack_start); /* 28 UINTX_FORMAT */
duke@0 1274 }
duke@0 1275
bobv@1601 1276 #undef _UFM
bobv@1601 1277 #undef _DFM
bobv@1601 1278
duke@0 1279 if (i != 28 - 2) {
duke@0 1280 assert(false, "Bad conversion from /proc/self/stat");
dbuck@8815 1281 // product mode - assume we are the primordial thread, good luck in the
duke@0 1282 // embedded case.
dbuck@8815 1283 warning("Can't detect primordial thread stack location - bad conversion");
duke@0 1284 stack_start = (uintptr_t) &rlim;
duke@0 1285 }
duke@0 1286 } else {
duke@0 1287 // For some reason we can't open /proc/self/stat (for example, running on
duke@0 1288 // FreeBSD with a Linux emulator, or inside chroot), this should work for
duke@0 1289 // most cases, so don't abort:
dbuck@8815 1290 warning("Can't detect primordial thread stack location - no /proc/self/stat");
duke@0 1291 stack_start = (uintptr_t) &rlim;
duke@0 1292 }
duke@0 1293 }
duke@0 1294
duke@0 1295 // Now we have a pointer (stack_start) very close to the stack top, the
duke@0 1296 // next thing to do is to figure out the exact location of stack top. We
duke@0 1297 // can find out the virtual memory area that contains stack_start by
duke@0 1298 // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
duke@0 1299 // and its upper limit is the real stack top. (again, this would fail if
duke@0 1300 // running inside chroot, because /proc may not exist.)
duke@0 1301
duke@0 1302 uintptr_t stack_top;
duke@0 1303 address low, high;
duke@0 1304 if (find_vma((address)stack_start, &low, &high)) {
duke@0 1305 // success, "high" is the true stack top. (ignore "low", because initial
duke@0 1306 // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
duke@0 1307 stack_top = (uintptr_t)high;
duke@0 1308 } else {
duke@0 1309 // failed, likely because /proc/self/maps does not exist
dbuck@8815 1310 warning("Can't detect primordial thread stack location - find_vma failed");
duke@0 1311 // best effort: stack_start is normally within a few pages below the real
duke@0 1312 // stack top, use it as stack top, and reduce stack size so we won't put
duke@0 1313 // guard page outside stack.
duke@0 1314 stack_top = stack_start;
duke@0 1315 stack_size -= 16 * page_size();
duke@0 1316 }
duke@0 1317
duke@0 1318 // stack_top could be partially down the page so align it
duke@0 1319 stack_top = align_size_up(stack_top, page_size());
duke@0 1320
dholmes@8236 1321 // Allowed stack value is minimum of max_size and what we derived from rlimit
dholmes@8236 1322 if (max_size > 0) {
dholmes@8236 1323 _initial_thread_stack_size = MIN2(max_size, stack_size);
duke@0 1324 } else {
dholmes@8236 1325 // Accept the rlimit max, but if stack is unlimited then it will be huge, so
dholmes@8236 1326 // clamp it at 8MB as we do on Solaris
dholmes@8236 1327 _initial_thread_stack_size = MIN2(stack_size, 8*M);
duke@0 1328 }
duke@0 1329
duke@0 1330 _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());
duke@0 1331 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;
dholmes@8236 1332 assert(_initial_thread_stack_bottom < (address)stack_top, "overflow!");
duke@0 1333 }
duke@0 1334
duke@0 1335 ////////////////////////////////////////////////////////////////////////////////
duke@0 1336 // time support
duke@0 1337
duke@0 1338 // Time since start-up in seconds to a fine granularity.
duke@0 1339 // Used by VMSelfDestructTimer and the MemProfiler.
duke@0 1340 double os::elapsedTime() {
duke@0 1341
jbachorik@5591 1342 return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution
duke@0 1343 }
duke@0 1344
duke@0 1345 jlong os::elapsed_counter() {
jbachorik@5591 1346 return javaTimeNanos() - initial_time_count;
duke@0 1347 }
duke@0 1348
duke@0 1349 jlong os::elapsed_frequency() {
jbachorik@5591 1350 return NANOSECS_PER_SEC; // nanosecond resolution
duke@0 1351 }
duke@0 1352
tschatzl@4769 1353 bool os::supports_vtime() { return true; }
ysr@342 1354 bool os::enable_vtime() { return false; }
ysr@342 1355 bool os::vtime_enabled() { return false; }
tschatzl@4769 1356
ysr@342 1357 double os::elapsedVTime() {
tschatzl@4769 1358 struct rusage usage;
tschatzl@4769 1359 int retval = getrusage(RUSAGE_THREAD, &usage);
tschatzl@4769 1360 if (retval == 0) {
tschatzl@4769 1361 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);
tschatzl@4769 1362 } else {
tschatzl@4769 1363 // better than nothing, but not much
tschatzl@4769 1364 return elapsedTime();
tschatzl@4769 1365 }
ysr@342 1366 }
ysr@342 1367
sbohne@61 1368 jlong os::javaTimeMillis() {
duke@0 1369 timeval time;
duke@0 1370 int status = gettimeofday(&time, NULL);
duke@0 1371 assert(status != -1, "linux error");
duke@0 1372 return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);
duke@0 1373 }
duke@0 1374
duke@0 1375 #ifndef CLOCK_MONOTONIC
duke@0 1376 #define CLOCK_MONOTONIC (1)
duke@0 1377 #endif
duke@0 1378
duke@0 1379 void os::Linux::clock_init() {
duke@0 1380 // we do dlopen's in this particular order due to bug in linux
duke@0 1381 // dynamical loader (see 6348968) leading to crash on exit
duke@0 1382 void* handle = dlopen("librt.so.1", RTLD_LAZY);
duke@0 1383 if (handle == NULL) {
duke@0 1384 handle = dlopen("librt.so", RTLD_LAZY);
duke@0 1385 }
duke@0 1386
duke@0 1387 if (handle) {
duke@0 1388 int (*clock_getres_func)(clockid_t, struct timespec*) =
duke@0 1389 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
duke@0 1390 int (*clock_gettime_func)(clockid_t, struct timespec*) =
duke@0 1391 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
duke@0 1392 if (clock_getres_func && clock_gettime_func) {
duke@0 1393 // See if monotonic clock is supported by the kernel. Note that some
duke@0 1394 // early implementations simply return kernel jiffies (updated every
duke@0 1395 // 1/100 or 1/1000 second). It would be bad to use such a low res clock
duke@0 1396 // for nano time (though the monotonic property is still nice to have).
duke@0 1397 // It's fixed in newer kernels, however clock_getres() still returns
duke@0 1398 // 1/HZ. We check if clock_getres() works, but will ignore its reported
duke@0 1399 // resolution for now. Hopefully as people move to new kernels, this
duke@0 1400 // won't be a problem.
duke@0 1401 struct timespec res;
duke@0 1402 struct timespec tp;
duke@0 1403 if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&
duke@0 1404 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
duke@0 1405 // yes, monotonic clock is supported
duke@0 1406 _clock_gettime = clock_gettime_func;
dholmes@5244 1407 return;
duke@0 1408 } else {
duke@0 1409 // close librt if there is no monotonic clock
duke@0 1410 dlclose(handle);
duke@0 1411 }
duke@0 1412 }
duke@0 1413 }
dholmes@5244 1414 warning("No monotonic clock was available - timed services may " \
dholmes@5244 1415 "be adversely affected if the time-of-day clock changes");
duke@0 1416 }
duke@0 1417
duke@0 1418 #ifndef SYS_clock_getres
duke@0 1419
duke@0 1420 #if defined(IA32) || defined(AMD64)
duke@0 1421 #define SYS_clock_getres IA32_ONLY(266) AMD64_ONLY(229)
bobv@1601 1422 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
duke@0 1423 #else
bobv@1601 1424 #warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time"
bobv@1601 1425 #define sys_clock_getres(x,y) -1
duke@0 1426 #endif
duke@0 1427
bobv@1601 1428 #else
bobv@1601 1429 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
duke@0 1430 #endif
duke@0 1431
duke@0 1432 void os::Linux::fast_thread_clock_init() {
duke@0 1433 if (!UseLinuxPosixThreadCPUClocks) {
duke@0 1434 return;
duke@0 1435 }
duke@0 1436 clockid_t clockid;
duke@0 1437 struct timespec tp;
duke@0 1438 int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
duke@0 1439 (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
duke@0 1440
duke@0 1441 // Switch to using fast clocks for thread cpu time if
duke@0 1442 // the sys_clock_getres() returns 0 error code.
duke@0 1443 // Note, that some kernels may support the current thread
duke@0 1444 // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
duke@0 1445 // returned by the pthread_getcpuclockid().
duke@0 1446 // If the fast Posix clocks are supported then the sys_clock_getres()
duke@0 1447 // must return at least tp.tv_sec == 0 which means a resolution
duke@0 1448 // better than 1 sec. This is extra check for reliability.
duke@0 1449
duke@0 1450 if(pthread_getcpuclockid_func &&
duke@0 1451 pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
duke@0 1452 sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
duke@0 1453
duke@0 1454 _supports_fast_thread_cpu_time = true;
duke@0 1455 _pthread_getcpuclockid = pthread_getcpuclockid_func;
duke@0 1456 }
duke@0 1457 }
duke@0 1458
duke@0 1459 jlong os::javaTimeNanos() {
duke@0 1460 if (Linux::supports_monotonic_clock()) {
duke@0 1461 struct timespec tp;
duke@0 1462 int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
duke@0 1463 assert(status == 0, "gettime error");
duke@0 1464 jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
duke@0 1465 return result;
duke@0 1466 } else {
duke@0 1467 timeval time;
duke@0 1468 int status = gettimeofday(&time, NULL);
duke@0 1469 assert(status != -1, "linux error");
duke@0 1470 jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
duke@0 1471 return 1000 * usecs;
duke@0 1472 }
duke@0 1473 }
duke@0 1474
duke@0 1475 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
duke@0 1476 if (Linux::supports_monotonic_clock()) {
duke@0 1477 info_ptr->max_value = ALL_64_BITS;
duke@0 1478
duke@0 1479 // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
duke@0 1480 info_ptr->may_skip_backward = false; // not subject to resetting or drifting
duke@0 1481 info_ptr->may_skip_forward = false; // not subject to resetting or drifting
duke@0 1482 } else {
duke@0 1483 // gettimeofday - based on time in seconds since the Epoch thus does not wrap
duke@0 1484 info_ptr->max_value = ALL_64_BITS;
duke@0 1485
duke@0 1486 // gettimeofday is a real time clock so it skips
duke@0 1487 info_ptr->may_skip_backward = true;
duke@0 1488 info_ptr->may_skip_forward = true;
duke@0 1489 }
duke@0 1490
duke@0 1491 info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time
duke@0 1492 }
duke@0 1493
duke@0 1494 // Return the real, user, and system times in seconds from an
duke@0 1495 // arbitrary fixed point in the past.
duke@0 1496 bool os::getTimesSecs(double* process_real_time,
duke@0 1497 double* process_user_time,
duke@0 1498 double* process_system_time) {
duke@0 1499 struct tms ticks;
duke@0 1500 clock_t real_ticks = times(&ticks);
duke@0 1501
duke@0 1502 if (real_ticks == (clock_t) (-1)) {
duke@0 1503 return false;
duke@0 1504 } else {
duke@0 1505 double ticks_per_second = (double) clock_tics_per_sec;
duke@0 1506 *process_user_time = ((double) ticks.tms_utime) / ticks_per_second;
duke@0 1507 *process_system_time = ((double) ticks.tms_stime) / ticks_per_second;
duke@0 1508 *process_real_time = ((double) real_ticks) / ticks_per_second;
duke@0 1509
duke@0 1510 return true;
duke@0 1511 }
duke@0 1512 }
duke@0 1513
duke@0 1514
duke@0 1515 char * os::local_time_string(char *buf, size_t buflen) {
duke@0 1516 struct tm t;
duke@0 1517 time_t long_time;
duke@0 1518 time(&long_time);
duke@0 1519 localtime_r(&long_time, &t);
duke@0 1520 jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
duke@0 1521 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
duke@0 1522 t.tm_hour, t.tm_min, t.tm_sec);
duke@0 1523 return buf;
duke@0 1524 }
duke@0 1525
ysr@548 1526 struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {
ysr@548 1527 return localtime_r(clock, res);
ysr@548 1528 }
ysr@548 1529
duke@0 1530 ////////////////////////////////////////////////////////////////////////////////
duke@0 1531 // runtime exit support
duke@0 1532
duke@0 1533 // Note: os::shutdown() might be called very early during initialization, or
duke@0 1534 // called from signal handler. Before adding something to os::shutdown(), make
duke@0 1535 // sure it is async-safe and can handle partially initialized VM.
duke@0 1536 void os::shutdown() {
duke@0 1537
duke@0 1538 // allow PerfMemory to attempt cleanup of any persistent resources
duke@0 1539 perfMemory_exit();
duke@0 1540
duke@0 1541 // needs to remove object in file system
duke@0 1542 AttachListener::abort();
duke@0 1543
duke@0 1544 // flush buffered output, finish log files
duke@0 1545 ostream_abort();
duke@0 1546
duke@0 1547 // Check for abort hook
duke@0 1548 abort_hook_t abort_hook = Arguments::abort_hook();
duke@0 1549 if (abort_hook != NULL) {
duke@0 1550 abort_hook();
duke@0 1551 }
duke@0 1552
duke@0 1553 }
duke@0 1554
duke@0 1555 // Note: os::abort() might be called very early during initialization, or
duke@0 1556 // called from signal handler. Before adding something to os::abort(), make
duke@0 1557 // sure it is async-safe and can handle partially initialized VM.
duke@0 1558 void os::abort(bool dump_core) {
duke@0 1559 os::shutdown();
duke@0 1560 if (dump_core) {
duke@0 1561 #ifndef PRODUCT
duke@0 1562 fdStream out(defaultStream::output_fd());
duke@0 1563 out.print_raw("Current thread is ");
duke@0 1564 char buf[16];
duke@0 1565 jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
duke@0 1566 out.print_raw_cr(buf);
duke@0 1567 out.print_raw_cr("Dumping core ...");
duke@0 1568 #endif
duke@0 1569 ::abort(); // dump core
duke@0 1570 }
duke@0 1571
duke@0 1572 ::exit(1);
duke@0 1573 }
duke@0 1574
duke@0 1575 // Die immediately, no exit hook, no abort hook, no cleanup.
duke@0 1576 void os::die() {
duke@0 1577 // _exit() on LinuxThreads only kills current thread
duke@0 1578 ::abort();
duke@0 1579 }
duke@0 1580
ikrylov@1887 1581
ikrylov@1887 1582 // This method is a copy of JDK's sysGetLastErrorString
ikrylov@1887 1583 // from src/solaris/hpi/src/system_md.c
ikrylov@1887 1584
ikrylov@1887 1585 size_t os::lasterror(char *buf, size_t len) {
ikrylov@1887 1586
ikrylov@1887 1587 if (errno == 0) return 0;
ikrylov@1887 1588
ikrylov@1887 1589 const char *s = ::strerror(errno);
ikrylov@1887 1590 size_t n = ::strlen(s);
ikrylov@1887 1591 if (n >= len) {
ikrylov@1887 1592 n = len - 1;
ikrylov@1887 1593 }
ikrylov@1887 1594 ::strncpy(buf, s, n);
ikrylov@1887 1595 buf[n] = '\0';
ikrylov@1887 1596 return n;
ikrylov@1887 1597 }
ikrylov@1887 1598
duke@0 1599 intx os::current_thread_id() { return (intx)pthread_self(); }
duke@0 1600 int os::current_process_id() {
duke@0 1601
duke@0 1602 // Under the old linux thread library, linux gives each thread
duke@0 1603 // its own process id. Because of this each thread will return
duke@0 1604 // a different pid if this method were to return the result
duke@0 1605 // of getpid(2). Linux provides no api that returns the pid
duke@0 1606 // of the launcher thread for the vm. This implementation
duke@0 1607 // returns a unique pid, the pid of the launcher thread
duke@0 1608 // that starts the vm 'process'.
duke@0 1609
duke@0 1610 // Under the NPTL, getpid() returns the same pid as the
duke@0 1611 // launcher thread rather than a unique pid per thread.
duke@0 1612 // Use gettid() if you want the old pre NPTL behaviour.
duke@0 1613
duke@0 1614 // if you are looking for the result of a call to getpid() that
duke@0 1615 // returns a unique pid for the calling thread, then look at the
duke@0 1616 // OSThread::thread_id() method in osThread_linux.hpp file
duke@0 1617
duke@0 1618 return (int)(_initial_pid ? _initial_pid : getpid());
duke@0 1619 }
duke@0 1620
duke@0 1621 // DLL functions
duke@0 1622
duke@0 1623 const char* os::dll_file_extension() { return ".so"; }
duke@0 1624
coleenp@2015 1625 // This must be hard coded because it's the system's temporary
coleenp@2015 1626 // directory not the java application's temp directory, ala java.io.tmpdir.
coleenp@2015 1627 const char* os::get_temp_directory() { return "/tmp"; }
duke@0 1628
phh@691 1629 static bool file_exists(const char* filename) {
phh@691 1630 struct stat statbuf;
phh@691 1631 if (filename == NULL || strlen(filename) == 0) {
phh@691 1632 return false;
phh@691 1633 }
phh@691 1634 return os::stat(filename, &statbuf) == 0;
phh@691 1635 }
phh@691 1636
bpittore@3826 1637 bool os::dll_build_name(char* buffer, size_t buflen,
phh@691 1638 const char* pname, const char* fname) {
bpittore@3826 1639 bool retval = false;
phh@691 1640 // Copied from libhpi
kamg@242 1641 const size_t pnamelen = pname ? strlen(pname) : 0;
kamg@242 1642
bpittore@3826 1643 // Return error on buffer overflow.
kamg@242 1644 if (pnamelen + strlen(fname) + 10 > (size_t) buflen) {
bpittore@3826 1645 return retval;
kamg@242 1646 }
kamg@242 1647
kamg@242 1648 if (pnamelen == 0) {
phh@691 1649 snprintf(buffer, buflen, "lib%s.so", fname);
bpittore@3826 1650 retval = true;
phh@691 1651 } else if (strchr(pname, *os::path_separator()) != NULL) {
phh@691 1652 int n;
phh@691 1653 char** pelements = split_path(pname, &n);
ccheung@4453 1654 if (pelements == NULL) {
dcubed@4456 1655 return false;
ccheung@4453 1656 }
phh@691 1657 for (int i = 0 ; i < n ; i++) {
phh@691 1658 // Really shouldn't be NULL, but check can't hurt
phh@691 1659 if (pelements[i] == NULL || strlen(pelements[i]) == 0) {
phh@691 1660 continue; // skip the empty path values
phh@691 1661 }
phh@691 1662 snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname);
phh@691 1663 if (file_exists(buffer)) {
bpittore@3826 1664 retval = true;
phh@691 1665 break;
phh@691 1666 }
phh@691 1667 }
phh@691 1668 // release the storage
phh@691 1669 for (int i = 0 ; i < n ; i++) {
phh@691 1670 if (pelements[i] != NULL) {
zgu@3465 1671 FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);
phh@691 1672 }
phh@691 1673 }
phh@691 1674 if (pelements != NULL) {
zgu@3465 1675 FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);
phh@691 1676 }
kamg@242 1677 } else {
phh@691 1678 snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);
bpittore@3826 1679 retval = true;
bpittore@3826 1680 }
bpittore@3826 1681 return retval;
kamg@242 1682 }
kamg@242 1683
dcubed@3957 1684 // check if addr is inside libjvm.so
duke@0 1685 bool os::address_is_in_vm(address addr) {
duke@0 1686 static address libjvm_base_addr;
duke@0 1687 Dl_info dlinfo;
duke@0 1688
duke@0 1689 if (libjvm_base_addr == NULL) {
dcubed@4930 1690 if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {
dcubed@4930 1691 libjvm_base_addr = (address)dlinfo.dli_fbase;
dcubed@4930 1692 }
duke@0 1693 assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
duke@0 1694 }
duke@0 1695
dcubed@4930 1696 if (dladdr((void *)addr, &dlinfo) != 0) {
duke@0 1697 if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
duke@0 1698 }
duke@0 1699
duke@0 1700 return false;
duke@0 1701 }
duke@0 1702
duke@0 1703 bool os::dll_address_to_function_name(address addr, char *buf,
duke@0 1704 int buflen, int *offset) {
dcubed@4930 1705 // buf is not optional, but offset is optional
dcubed@4930 1706 assert(buf != NULL, "sanity check");
dcubed@4930 1707
duke@0 1708 Dl_info dlinfo;
duke@0 1709
dcubed@4930 1710 if (dladdr((void*)addr, &dlinfo) != 0) {
dcubed@4930 1711 // see if we have a matching symbol
dcubed@4930 1712 if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
dcubed@4930 1713 if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {
zgu@1929 1714 jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
zgu@1929 1715 }
dcubed@4930 1716 if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
dcubed@4930 1717 return true;
zgu@1929 1718 }
dcubed@4930 1719 // no matching symbol so try for just file info
dcubed@4930 1720 if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
dcubed@4930 1721 if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
dcubed@4930 1722 buf, buflen, offset, dlinfo.dli_fname)) {
dcubed@4930 1723 return true;
dcubed@4930 1724 }
zgu@1929 1725 }
duke@0 1726 }
zgu@1929 1727
dcubed@4930 1728 buf[0] = '\0';
zgu@1929 1729 if (offset != NULL) *offset = -1;
zgu@1929 1730 return false;
duke@0 1731 }
duke@0 1732
duke@0 1733 struct _address_to_library_name {
duke@0 1734 address addr; // input : memory address
duke@0 1735 size_t buflen; // size of fname
duke@0 1736 char* fname; // output: library name
duke@0 1737 address base; // library base addr
duke@0 1738 };
duke@0 1739
duke@0 1740 static int address_to_library_name_callback(struct dl_phdr_info *info,
duke@0 1741 size_t size, void *data) {
duke@0 1742 int i;
duke@0 1743 bool found = false;
duke@0 1744 address libbase = NULL;
duke@0 1745 struct _address_to_library_name * d = (struct _address_to_library_name *)data;
duke@0 1746
duke@0 1747 // iterate through all loadable segments
duke@0 1748 for (i = 0; i < info->dlpi_phnum; i++) {
duke@0 1749 address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
duke@0 1750 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
duke@0 1751 // base address of a library is the lowest address of its loaded
duke@0 1752 // segments.
duke@0 1753 if (libbase == NULL || libbase > segbase) {
duke@0 1754 libbase = segbase;
duke@0 1755 }
duke@0 1756 // see if 'addr' is within current segment
duke@0 1757 if (segbase <= d->addr &&
duke@0 1758 d->addr < segbase + info->dlpi_phdr[i].p_memsz) {
duke@0 1759 found = true;
duke@0 1760 }
duke@0 1761 }
duke@0 1762 }
duke@0 1763
duke@0 1764 // dlpi_name is NULL or empty if the ELF file is executable, return 0
duke@0 1765 // so dll_address_to_library_name() can fall through to use dladdr() which
duke@0 1766 // can figure out executable name from argv[0].
duke@0 1767 if (found && info->dlpi_name && info->dlpi_name[0]) {
duke@0 1768 d->base = libbase;
duke@0 1769 if (d->fname) {
duke@0 1770 jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
duke@0 1771 }
duke@0 1772 return 1;
duke@0 1773 }
duke@0 1774 return 0;
duke@0 1775 }
duke@0 1776
duke@0 1777 bool os::dll_address_to_library_name(address addr, char* buf,
duke@0 1778 int buflen, int* offset) {
dcubed@4930 1779 // buf is not optional, but offset is optional
dcubed@4930 1780 assert(buf != NULL, "sanity check");
dcubed@4930 1781
duke@0 1782 Dl_info dlinfo;
duke@0 1783 struct _address_to_library_name data;
duke@0 1784
duke@0 1785 // There is a bug in old glibc dladdr() implementation that it could resolve
duke@0 1786 // to wrong library name if the .so file has a base address != NULL. Here
duke@0 1787 // we iterate through the program headers of all loaded libraries to find
duke@0 1788 // out which library 'addr' really belongs to. This workaround can be
duke@0 1789 // removed once the minimum requirement for glibc is moved to 2.3.x.
duke@0 1790 data.addr = addr;
duke@0 1791 data.fname = buf;
duke@0 1792 data.buflen = buflen;
duke@0 1793 data.base = NULL;
duke@0 1794 int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
duke@0 1795
duke@0 1796 if (rslt) {
duke@0 1797 // buf already contains library name
duke@0 1798 if (offset) *offset = addr - data.base;
duke@0 1799 return true;
dcubed@4930 1800 }
dcubed@4930 1801 if (dladdr((void*)addr, &dlinfo) != 0) {
dcubed@4930 1802 if (dlinfo.dli_fname != NULL) {
dcubed@4930 1803 jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
dcubed@4930 1804 }
dcubed@4930 1805 if (dlinfo.dli_fbase != NULL && offset != NULL) {
dcubed@4930 1806 *offset = addr - (address)dlinfo.dli_fbase;
dcubed@4930 1807 }
dcubed@4930 1808 return true;
dcubed@4930 1809 }
dcubed@4930 1810
dcubed@4930 1811 buf[0] = '\0';
dcubed@4930 1812 if (offset) *offset = -1;
dcubed@4930 1813 return false;
duke@0 1814 }
duke@0 1815
duke@0 1816 // Loads .dll/.so and
duke@0 1817 // in case of error it checks if .dll/.so was built for the
duke@0 1818 // same architecture as Hotspot is running on
duke@0 1819
iklam@4275 1820
iklam@4275 1821 // Remember the stack's state. The Linux dynamic linker will change
iklam@4275 1822 // the stack to 'executable' at most once, so we must safepoint only once.
iklam@4275 1823 bool os::Linux::_stack_is_executable = false;
iklam@4275 1824
iklam@4275 1825 // VM operation that loads a library. This is necessary if stack protection
iklam@4275 1826 // of the Java stacks can be lost during loading the library. If we
iklam@4275 1827 // do not stop the Java threads, they can stack overflow before the stacks
iklam@4275 1828 // are protected again.
iklam@4275 1829 class VM_LinuxDllLoad: public VM_Operation {
iklam@4275 1830 private:
iklam@4275 1831 const char *_filename;
iklam@4377 1832 char *_ebuf;
iklam@4377 1833 int _ebuflen;
iklam@4275 1834 void *_lib;
iklam@4275 1835 public:
iklam@4377 1836 VM_LinuxDllLoad(const char *fn, char *ebuf, int ebuflen) :
iklam@4377 1837 _filename(fn), _ebuf(ebuf), _ebuflen(ebuflen), _lib(NULL) {}
iklam@4275 1838 VMOp_Type type() const { return VMOp_LinuxDllLoad; }
iklam@4275 1839 void doit() {
iklam@4377 1840 _lib = os::Linux::dll_load_in_vmthread(_filename, _ebuf, _ebuflen);
iklam@4275 1841 os::Linux::_stack_is_executable = true;
iklam@4275 1842 }
iklam@4275 1843 void* loaded_library() { return _lib; }
iklam@4275 1844 };
iklam@4275 1845
duke@0 1846 void * os::dll_load(const char *filename, char *ebuf, int ebuflen)
duke@0 1847 {
iklam@4275 1848 void * result = NULL;
iklam@4275 1849 bool load_attempted = false;
iklam@4275 1850
iklam@4275 1851 // Check whether the library to load might change execution rights
iklam@4275 1852 // of the stack. If they are changed, the protection of the stack
iklam@4275 1853 // guard pages will be lost. We need a safepoint to fix this.
iklam@4275 1854 //
iklam@4275 1855 // See Linux man page execstack(8) for more info.
iklam@4275 1856 if (os::uses_stack_guard_pages() && !os::Linux::_stack_is_executable) {
iklam@4275 1857 ElfFile ef(filename);
iklam@4275 1858 if (!ef.specifies_noexecstack()) {
iklam@4275 1859 if (!is_init_completed()) {
iklam@4275 1860 os::Linux::_stack_is_executable = true;
iklam@4275 1861 // This is OK - No Java threads have been created yet, and hence no
iklam@4275 1862 // stack guard pages to fix.
iklam@4275 1863 //
iklam@4275 1864 // This should happen only when you are building JDK7 using a very
iklam@4275 1865 // old version of JDK6 (e.g., with JPRT) and running test_gamma.
iklam@4275 1866 //
iklam@4275 1867 // Dynamic loader will make all stacks executable after
iklam@4275 1868 // this function returns, and will not do that again.
iklam@4275 1869 assert(Threads::first() == NULL, "no Java threads should exist yet.");
iklam@4275 1870 } else {
iklam@4275 1871 warning("You have loaded library %s which might have disabled stack guard. "
iklam@4275 1872 "The VM will try to fix the stack guard now.\n"
iklam@4275 1873 "It's highly recommended that you fix the library with "
iklam@4275 1874 "'execstack -c <libfile>', or link it with '-z noexecstack'.",
iklam@4275 1875 filename);
iklam@4275 1876
iklam@4275 1877 assert(Thread::current()->is_Java_thread(), "must be Java thread");
iklam@4275 1878 JavaThread *jt = JavaThread::current();
iklam@4275 1879 if (jt->thread_state() != _thread_in_native) {
iklam@4275 1880 // This happens when a compiler thread tries to load a hsdis-<arch>.so file
iklam@4275 1881 // that requires ExecStack. Cannot enter safe point. Let's give up.
iklam@4275 1882 warning("Unable to fix stack guard. Giving up.");
iklam@4275 1883 } else {
iklam@4275 1884 if (!LoadExecStackDllInVMThread) {
iklam@4275 1885 // This is for the case where the DLL has an static
iklam@4275 1886 // constructor function that executes JNI code. We cannot
iklam@4275 1887 // load such DLLs in the VMThread.
iklam@4377 1888 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);
iklam@4275 1889 }
iklam@4275 1890
iklam@4275 1891 ThreadInVMfromNative tiv(jt);
iklam@4275 1892 debug_only(VMNativeEntryWrapper vew;)
iklam@4275 1893
iklam@4377 1894 VM_LinuxDllLoad op(filename, ebuf, ebuflen);
iklam@4275 1895 VMThread::execute(&op);
iklam@4275 1896 if (LoadExecStackDllInVMThread) {
iklam@4275 1897 result = op.loaded_library();
iklam@4275 1898 }
iklam@4275 1899 load_attempted = true;
iklam@4275 1900 }
iklam@4275 1901 }
iklam@4275 1902 }
iklam@4275 1903 }
iklam@4275 1904
iklam@4275 1905 if (!load_attempted) {
iklam@4377 1906 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);
iklam@4275 1907 }
iklam@4275 1908
duke@0 1909 if (result != NULL) {
duke@0 1910 // Successful loading
duke@0 1911 return result;
duke@0 1912 }
duke@0 1913
duke@0 1914 Elf32_Ehdr elf_head;
duke@0 1915 int diag_msg_max_length=ebuflen-strlen(ebuf);
duke@0 1916 char* diag_msg_buf=ebuf+strlen(ebuf);
duke@0 1917
duke@0 1918 if (diag_msg_max_length==0) {
duke@0 1919 // No more space in ebuf for additional diagnostics message
duke@0 1920 return NULL;
duke@0 1921 }
duke@0 1922
duke@0 1923
duke@0 1924 int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
duke@0 1925
duke@0 1926 if (file_descriptor < 0) {
duke@0 1927 // Can't open library, report dlerror() message
duke@0 1928 return NULL;
duke@0 1929 }
duke@0 1930
duke@0 1931 bool failed_to_read_elf_head=
duke@0 1932 (sizeof(elf_head)!=
duke@0 1933 (::read(file_descriptor, &elf_head,sizeof(elf_head)))) ;
duke@0 1934
duke@0 1935 ::close(file_descriptor);
duke@0 1936 if (failed_to_read_elf_head) {
duke@0 1937 // file i/o error - report dlerror() msg
duke@0 1938 return NULL;
duke@0 1939 }
duke@0 1940
duke@0 1941 typedef struct {
duke@0 1942 Elf32_Half code; // Actual value as defined in elf.h
duke@0 1943 Elf32_Half compat_class; // Compatibility of archs at VM's sense
duke@0 1944 char elf_class; // 32 or 64 bit
duke@0 1945 char endianess; // MSB or LSB
duke@0 1946 char* name; // String representation
duke@0 1947 } arch_t;
duke@0 1948
duke@0 1949 #ifndef EM_486
duke@0 1950 #define EM_486 6 /* Intel 80486 */
duke@0 1951 #endif
duke@0 1952
duke@0 1953 static const arch_t arch_array[]={
duke@0 1954 {EM_386, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
duke@0 1955 {EM_486, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
duke@0 1956 {EM_IA_64, EM_IA_64, ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
duke@0 1957 {EM_X86_64, EM_X86_64, ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
duke@0 1958 {EM_SPARC, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
duke@0 1959 {EM_SPARC32PLUS, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
duke@0 1960 {EM_SPARCV9, EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
duke@0 1961 {EM_PPC, EM_PPC, ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
kvn@6103 1962 #if defined(VM_LITTLE_ENDIAN)
sgehwolf@8976 1963 {EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2LSB, (char*)"Power PC 64 LE"},
kvn@6103 1964 #else
never@1010 1965 {EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
kvn@6103 1966 #endif
never@1010 1967 {EM_ARM, EM_ARM, ELFCLASS32, ELFDATA2LSB, (char*)"ARM"},
never@1010 1968 {EM_S390, EM_S390, ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},
never@1010 1969 {EM_ALPHA, EM_ALPHA, ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},
never@1010 1970 {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},
never@1010 1971 {EM_MIPS, EM_MIPS, ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},
never@1010 1972 {EM_PARISC, EM_PARISC, ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},
never@1010 1973 {EM_68K, EM_68K, ELFCLASS32, ELFDATA2MSB, (char*)"M68k"}
duke@0 1974 };
duke@0 1975
duke@0 1976 #if (defined IA32)
duke@0 1977 static Elf32_Half running_arch_code=EM_386;
duke@0 1978 #elif (defined AMD64)
duke@0 1979 static Elf32_Half running_arch_code=EM_X86_64;
duke@0 1980 #elif (defined IA64)
duke@0 1981 static Elf32_Half running_arch_code=EM_IA_64;
duke@0 1982 #elif (defined __sparc) && (defined _LP64)
duke@0 1983 static Elf32_Half running_arch_code=EM_SPARCV9;
duke@0 1984 #elif (defined __sparc) && (!defined _LP64)
duke@0 1985 static Elf32_Half running_arch_code=EM_SPARC;
duke@0 1986 #elif (defined __powerpc64__)
duke@0 1987 static Elf32_Half running_arch_code=EM_PPC64;
duke@0 1988 #elif (defined __powerpc__)
duke@0 1989 static Elf32_Half running_arch_code=EM_PPC;
never@1010 1990 #elif (defined ARM)
never@1010 1991 static Elf32_Half running_arch_code=EM_ARM;
never@1010 1992 #elif (defined S390)
never@1010 1993 static Elf32_Half running_arch_code=EM_S390;
never@1010 1994 #elif (defined ALPHA)
never@1010 1995 static Elf32_Half running_arch_code=EM_ALPHA;
never@1010 1996 #elif (defined MIPSEL)
never@1010 1997 static Elf32_Half running_arch_code=EM_MIPS_RS3_LE;
never@1010 1998 #elif (defined PARISC)
never@1010 1999 static Elf32_Half running_arch_code=EM_PARISC;
never@1010 2000 #elif (defined MIPS)
never@1010 2001 static Elf32_Half running_arch_code=EM_MIPS;
never@1010 2002 #elif (defined M68K)
never@1010 2003 static Elf32_Half running_arch_code=EM_68K;
duke@0 2004 #else
duke@0 2005 #error Method os::dll_load requires that one of following is defined:\
never@1010 2006 IA32, AMD64, IA64, __sparc, __powerpc__, ARM, S390, ALPHA, MIPS, MIPSEL, PARISC, M68K
duke@0 2007 #endif
duke@0 2008
duke@0 2009 // Identify compatability class for VM's architecture and library's architecture
duke@0 2010 // Obtain string descriptions for architectures
duke@0 2011
duke@0 2012 arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
duke@0 2013 int running_arch_index=-1;
duke@0 2014
duke@0 2015 for (unsigned int i=0 ; i < ARRAY_SIZE(arch_array) ; i++ ) {
duke@0 2016 if (running_arch_code == arch_array[i].code) {
duke@0 2017 running_arch_index = i;
duke@0 2018 }
duke@0 2019 if (lib_arch.code == arch_array[i].code) {
duke@0 2020 lib_arch.compat_class = arch_array[i].compat_class;
duke@0 2021 lib_arch.name = arch_array[i].name;
duke@0 2022 }
duke@0 2023 }
duke@0 2024
duke@0 2025 assert(running_arch_index != -1,
duke@0 2026 "Didn't find running architecture code (running_arch_code) in arch_array");
duke@0 2027 if (running_arch_index == -1) {
duke@0 2028 // Even though running architecture detection failed
duke@0 2029 // we may still continue with reporting dlerror() message
duke@0 2030 return NULL;
duke@0 2031 }
duke@0 2032
duke@0 2033 if (lib_arch.endianess != arch_array[running_arch_index].endianess) {
duke@0 2034 ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");
duke@0 2035 return NULL;
duke@0 2036 }
duke@0 2037
never@1010 2038 #ifndef S390
duke@0 2039 if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
duke@0 2040 ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");
duke@0 2041 return NULL;
duke@0 2042 }
never@1010 2043 #endif // !S390
duke@0 2044
duke@0 2045 if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
duke@0 2046 if ( lib_arch.name!=NULL ) {
duke@0 2047 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
duke@0 2048 " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
duke@0 2049 lib_arch.name, arch_array[running_arch_index].name);
duke@0 2050 } else {
duke@0 2051 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
duke@0 2052 " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
duke@0 2053 lib_arch.code,
duke@0 2054 arch_array[running_arch_index].name);
duke@0 2055 }
duke@0 2056 }
duke@0 2057
duke@0 2058 return NULL;
duke@0 2059 }
duke@0 2060
iklam@4377 2061 void * os::Linux::dlopen_helper(const char *filename, char *ebuf, int ebuflen) {
iklam@4377 2062 void * result = ::dlopen(filename, RTLD_LAZY);
iklam@4377 2063 if (result == NULL) {
iklam@4377 2064 ::strncpy(ebuf, ::dlerror(), ebuflen - 1);
iklam@4377 2065 ebuf[ebuflen-1] = '\0';
iklam@4377 2066 }
iklam@4377 2067 return result;
iklam@4377 2068 }
iklam@4377 2069
iklam@4377 2070 void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf, int ebuflen) {
iklam@4275 2071 void * result = NULL;
iklam@4275 2072 if (LoadExecStackDllInVMThread) {
iklam@4377 2073 result = dlopen_helper(filename, ebuf, ebuflen);
iklam@4275 2074 }
iklam@4275 2075
iklam@4275 2076 // Since 7019808, libjvm.so is linked with -noexecstack. If the VM loads a
iklam@4275 2077 // library that requires an executable stack, or which does not have this
iklam@4275 2078 // stack attribute set, dlopen changes the stack attribute to executable. The
iklam@4275 2079 // read protection of the guard pages gets lost.
iklam@4275 2080 //
iklam@4275 2081 // Need to check _stack_is_executable again as multiple VM_LinuxDllLoad
iklam@4275 2082 // may have been queued at the same time.
iklam@4275 2083
iklam@4275 2084 if (!_stack_is_executable) {
iklam@4275 2085 JavaThread *jt = Threads::first();
iklam@4275 2086
iklam@4275 2087 while (jt) {
iklam@4275 2088 if (!jt->stack_guard_zone_unused() && // Stack not yet fully initialized
iklam@4275 2089 jt->stack_yellow_zone_enabled()) { // No pending stack overflow exceptions
iklam@4275 2090 if (!os::guard_memory((char *) jt->stack_red_zone_base() - jt->stack_red_zone_size(),
iklam@4275 2091 jt->stack_yellow_zone_size() + jt->stack_red_zone_size())) {
iklam@4275 2092 warning("Attempt to reguard stack yellow zone failed.");
iklam@4275 2093 }
iklam@4275 2094 }
iklam@4275 2095 jt = jt->next();
iklam@4275 2096 }
iklam@4275 2097 }
iklam@4275 2098
iklam@4275 2099 return result;
iklam@4275 2100 }
iklam@4275 2101
kamg@242 2102 /*
kamg@242 2103 * glibc-2.0 libdl is not MT safe. If you are building with any glibc,
kamg@242 2104 * chances are you might want to run the generated bits against glibc-2.0
kamg@242 2105 * libdl.so, so always use locking for any version of glibc.
kamg@242 2106 */
kamg@242 2107 void* os::dll_lookup(void* handle, const char* name) {
kamg@242 2108 pthread_mutex_lock(&dl_mutex);
kamg@242 2109 void* res = dlsym(handle, name);
kamg@242 2110 pthread_mutex_unlock(&dl_mutex);
kamg@242 2111 return res;
kamg@242 2112 }
duke@0 2113
sla@5891 2114 void* os::get_default_process_handle() {
sla@5891 2115 return (void*)::dlopen(NULL, RTLD_LAZY);
sla@5891 2116 }
duke@0 2117
ikrylov@1887 2118 static bool _print_ascii_file(const char* filename, outputStream* st) {
ikrylov@1887 2119 int fd = ::open(filename, O_RDONLY);
duke@0 2120 if (fd == -1) {
duke@0 2121 return false;
duke@0 2122 }
duke@0 2123
duke@0 2124 char buf[32];
duke@0 2125 int bytes;
ikrylov@1887 2126 while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
duke@0 2127 st->print_raw(buf, bytes);
duke@0 2128 }
duke@0 2129
ikrylov@1887 2130 ::close(fd);
duke@0 2131
duke@0 2132 return true;
duke@0 2133 }
duke@0 2134
duke@0 2135 void os::print_dll_info(outputStream *st) {
duke@0 2136 st->print_cr("Dynamic libraries:");
duke@0 2137
duke@0 2138 char fname[32];
duke@0 2139 pid_t pid = os::Linux::gettid();
duke@0 2140
duke@0 2141 jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);
duke@0 2142
duke@0 2143 if (!_print_ascii_file(fname, st)) {
duke@0 2144 st->print("Can not get library information for pid = %d\n", pid);
duke@0 2145 }
duke@0 2146 }
duke@0 2147
nloodin@3348 2148 void os::print_os_info_brief(outputStream* st) {
nloodin@3348 2149 os::Linux::print_distro_info(st);
nloodin@3348 2150
nloodin@3348 2151 os::Posix::print_uname_info(st);
nloodin@3348 2152
nloodin@3348 2153 os::Linux::print_libversion_info(st);
nloodin@3348 2154
nloodin@3348 2155 }
duke@0 2156
duke@0 2157 void os::print_os_info(outputStream* st) {
duke@0 2158 st->print("OS:");
duke@0 2159
nloodin@3348 2160 os::Linux::print_distro_info(st);
nloodin@3348 2161
nloodin@3348 2162 os::Posix::print_uname_info(st);
nloodin@3348 2163
nloodin@3348 2164 // Print warning if unsafe chroot environment detected
nloodin@3348 2165 if (unsafe_chroot_detected) {
nloodin@3348 2166 st->print("WARNING!! ");
drchase@6245 2167 st->print_cr("%s", unstable_chroot_error);
nloodin@3348 2168 }
nloodin@3348 2169
nloodin@3348 2170 os::Linux::print_libversion_info(st);
nloodin@3348 2171
nloodin@3348 2172 os::Posix::print_rlimit_info(st);
nloodin@3348 2173
nloodin@3348 2174 os::Posix::print_load_average(st);
nloodin@3348 2175
nloodin@3348 2176 os::Linux::print_full_memory_info(st);
poonam@8787 2177
poonam@8787 2178 os::Linux::print_container_info(st);
nloodin@3348 2179 }
nloodin@3348 2180
nloodin@3348 2181 // Try to identify popular distros.
dcubed@5245 2182 // Most Linux distributions have a /etc/XXX-release file, which contains
dcubed@5245 2183 // the OS version string. Newer Linux distributions have a /etc/lsb-release
dcubed@5245 2184 // file that also contains the OS version string. Some have more than one
dcubed@5245 2185 // /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and
dcubed@5245 2186 // /etc/redhat-release.), so the order is important.
dcubed@5245 2187 // Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have
dcubed@5245 2188 // their own specific XXX-release file as well as a redhat-release file.
dcubed@5245 2189 // Because of this the XXX-release file needs to be searched for before the
dcubed@5245 2190 // redhat-release file.
dcubed@5245 2191 // Since Red Hat has a lsb-release file that is not very descriptive the
dcubed@5245 2192 // search for redhat-release needs to be before lsb-release.
dcubed@5245 2193 // Since the lsb-release file is the new standard it needs to be searched
dcubed@5245 2194 // before the older style release files.
dcubed@5245 2195 // Searching system-release (Red Hat) and os-release (other Linuxes) are a
dcubed@5245 2196 // next to last resort. The os-release file is a new standard that contains
dcubed@5245 2197 // distribution information and the system-release file seems to be an old
dcubed@5245 2198 // standard that has been replaced by the lsb-release and os-release files.
dcubed@5245 2199 // Searching for the debian_version file is the last resort. It contains
dcubed@5245 2200 // an informative string like "6.0.6" or "wheezy/sid". Because of this
dcubed@5245 2201 // "Debian " is printed before the contents of the debian_version file.
nloodin@3348 2202 void os::Linux::print_distro_info(outputStream* st) {
dcubed@5245 2203 if (!_print_ascii_file("/etc/oracle-release", st) &&
dcubed@5245 2204 !_print_ascii_file("/etc/mandriva-release", st) &&
dcubed@5245 2205 !_print_ascii_file("/etc/mandrake-release", st) &&
dcubed@5245 2206 !_print_ascii_file("/etc/sun-release", st) &&
dcubed@5245 2207 !_print_ascii_file("/etc/redhat-release", st) &&
dcubed@5245 2208 !_print_ascii_file("/etc/lsb-release", st) &&
dcubed@5245 2209 !_print_ascii_file("/etc/SuSE-release", st) &&
dcubed@5245 2210 !_print_ascii_file("/etc/turbolinux-release", st) &&
dcubed@5245 2211 !_print_ascii_file("/etc/gentoo-release", st) &&
dcubed@5245 2212 !_print_ascii_file("/etc/ltib-release", st) &&
dcubed@5245 2213 !_print_ascii_file("/etc/angstrom-version", st) &&
dcubed@5245 2214 !_print_ascii_file("/etc/system-release", st) &&
dcubed@5245 2215 !_print_ascii_file("/etc/os-release", st)) {
dcubed@5245 2216
dcubed@5245 2217 if (file_exists("/etc/debian_version")) {
dcubed@5245 2218 st->print("Debian ");
dcubed@5245 2219 _print_ascii_file("/etc/debian_version", st);
dcubed@5245 2220 } else {
dcubed@5245 2221 st->print("Linux");
dcubed@5245 2222 }
dcubed@5245 2223 }
dcubed@5245 2224 st->cr();
nloodin@3348 2225 }
nloodin@3348 2226
nloodin@3348 2227 void os::Linux::print_libversion_info(outputStream* st) {
duke@0 2228 // libc, pthread
duke@0 2229 st->print("libc:");
drchase@6245 2230 st->print("%s ", os::Linux::glibc_version());
drchase@6245 2231 st->print("%s ", os::Linux::libpthread_version());
duke@0 2232 if (os::Linux::is_LinuxThreads()) {
duke@0 2233 st->print("(%s stack)", os::Linux::is_floating_stack() ? "floating" : "fixed");
duke@0 2234 }
duke@0 2235 st->cr();
nloodin@3348 2236 }
nloodin@3348 2237
nloodin@3348 2238 void os::Linux::print_full_memory_info(outputStream* st) {
nloodin@3348 2239 st->print("\n/proc/meminfo:\n");
nloodin@3348 2240 _print_ascii_file("/proc/meminfo", st);
nloodin@3348 2241 st->cr();
jcoomes@2562 2242 }
jcoomes@2562 2243
poonam@8787 2244 void os::Linux::print_container_info(outputStream* st) {
poonam@8787 2245 if (!OSContainer::is_containerized()) {
poonam@8787 2246 return;
poonam@8787 2247 }
poonam@8787 2248
poonam@8787 2249 st->print("container (cgroup) information:\n");
poonam@8787 2250
poonam@8787 2251 const char *p_ct = OSContainer::container_type();
poonam@8787 2252 st->print("container_type: %s\n", p_ct != NULL ? p_ct : "failed");
poonam@8787 2253
poonam@8787 2254 char *p = OSContainer::cpu_cpuset_cpus();
poonam@8787 2255 st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "failed");
poonam@8787 2256 free(p);
poonam@8787 2257
poonam@8787 2258 p = OSContainer::cpu_cpuset_memory_nodes();
poonam@8787 2259 st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "failed");
poonam@8787 2260 free(p);
poonam@8787 2261
poonam@8787 2262 int i = OSContainer::active_processor_count();
poonam@8787 2263 if (i > 0) {
poonam@8787 2264 st->print("active_processor_count: %d\n", i);
poonam@8787 2265 } else {
poonam@8787 2266 st->print("active_processor_count: failed\n");
poonam@8787 2267 }
poonam@8787 2268
poonam@8787 2269 i = OSContainer::cpu_quota();
poonam@8787 2270 st->print("cpu_quota: %d\n", i);
poonam@8787 2271
poonam@8787 2272 i = OSContainer::cpu_period();
poonam@8787 2273 st->print("cpu_period: %d\n", i);
poonam@8787 2274
poonam@8787 2275 i = OSContainer::cpu_shares();
poonam@8787 2276 st->print("cpu_shares: %d\n", i);
poonam@8787 2277
poonam@8787 2278 jlong j = OSContainer::memory_limit_in_bytes();
poonam@8787 2279 st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);
poonam@8787 2280
poonam@8787 2281 j = OSContainer::memory_and_swap_limit_in_bytes();
poonam@8787 2282 st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);
poonam@8787 2283
poonam@8787 2284 j = OSContainer::memory_soft_limit_in_bytes();
poonam@8787 2285 st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);
poonam@8787 2286
poonam@8787 2287 j = OSContainer::OSContainer::memory_usage_in_bytes();
poonam@8787 2288 st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);
poonam@8787 2289
poonam@8787 2290 j = OSContainer::OSContainer::memory_max_usage_in_bytes();
poonam@8787 2291 st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
poonam@8787 2292 st->cr();
poonam@8787 2293 }
poonam@8787 2294
duke@0 2295 void os::print_memory_info(outputStream* st) {
duke@0 2296
duke@0 2297 st->print("Memory:");
duke@0 2298 st->print(" %dk page", os::vm_page_size()>>10);
duke@0 2299
duke@0 2300 // values in struct sysinfo are "unsigned long"
duke@0 2301 struct sysinfo si;
duke@0 2302 sysinfo(&si);
duke@0 2303
duke@0 2304 st->print(", physical " UINT64_FORMAT "k",
duke@0 2305 os::physical_memory() >> 10);
duke@0 2306 st->print("(" UINT64_FORMAT "k free)",
duke@0 2307 os::available_memory() >> 10);
duke@0 2308 st->print(", swap " UINT64_FORMAT "k",
duke@0 2309 ((jlong)si.totalswap * si.mem_unit) >> 10);
duke@0 2310 st->print("(" UINT64_FORMAT "k free)",
duke@0 2311 ((jlong)si.freeswap * si.mem_unit) >> 10);
duke@0 2312 st->cr();
duke@0 2313 }
duke@0 2314
nloodin@3348 2315 void os::pd_print_cpu_info(outputStream* st) {
nloodin@3348 2316 st->print("\n/proc/cpuinfo:\n");
nloodin@3348 2317 if (!_print_ascii_file("/proc/cpuinfo", st)) {
nloodin@3348 2318 st->print(" <Not Available>");
nloodin@3348 2319 }
nloodin@3348 2320 st->cr();
nloodin@3348 2321 }
nloodin@3348 2322
duke@0 2323 void os::print_siginfo(outputStream* st, void* siginfo) {
goetz@6025 2324 const siginfo_t* si = (const siginfo_t*)siginfo;
goetz@6025 2325
goetz@6025 2326 os::Posix::print_siginfo_brief(st, si);
iklam@6573 2327 #if INCLUDE_CDS
goetz@6025 2328 if (si && (si->si_signo == SIGBUS || si->si_signo == SIGSEGV) &&
duke@0 2329 UseSharedSpaces) {
duke@0 2330 FileMapInfo* mapinfo = FileMapInfo::current_info();
duke@0 2331 if (mapinfo->is_in_shared_space(si->si_addr)) {
duke@0 2332 st->print("\n\nError accessing class data sharing archive." \
duke@0 2333 " Mapped file inaccessible during execution, " \
duke@0 2334 " possible disk/network problem.");
duke@0 2335 }
duke@0 2336 }
iklam@6573 2337 #endif
duke@0 2338 st->cr();
duke@0 2339 }
duke@0 2340
duke@0 2341
duke@0 2342 static void print_signal_handler(outputStream* st, int sig,
duke@0 2343 char* buf, size_t buflen);
duke@0 2344
duke@0 2345 void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
duke@0 2346 st->print_cr("Signal Handlers:");
duke@0 2347 print_signal_handler(st, SIGSEGV, buf, buflen);
duke@0 2348 print_signal_handler(st, SIGBUS , buf, buflen);
duke@0 2349 print_signal_handler(st, SIGFPE , buf, buflen);
duke@0 2350 print_signal_handler(st, SIGPIPE, buf, buflen);
duke@0 2351 print_signal_handler(st, SIGXFSZ, buf, buflen);
duke@0 2352 print_signal_handler(st, SIGILL , buf, buflen);
duke@0 2353 print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen);
duke@0 2354 print_signal_handler(st, SR_signum, buf, buflen);
duke@0 2355 print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen);
duke@0 2356 print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);
duke@0 2357 print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen);
duke@0 2358 print_signal_handler(st, BREAK_SIGNAL, buf, buflen);
goetz@6047 2359 #if defined(PPC64)
goetz@6047 2360 print_signal_handler(st, SIGTRAP, buf, buflen);
goetz@6047 2361 #endif
duke@0 2362 }
duke@0 2363
duke@0 2364 static char saved_jvm_path[MAXPATHLEN] = {0};
duke@0 2365
dcubed@3957 2366 // Find the full path to the current module, libjvm.so
mchung@1562 2367 void os::jvm_path(char *buf, jint buflen) {
duke@0 2368 // Error checking.
mchung@1562 2369 if (buflen < MAXPATHLEN) {
duke@0 2370 assert(false, "must use a large-enough buffer");
duke@0 2371 buf[0] = '\0';
duke@0 2372 return;
duke@0 2373 }
duke@0 2374 // Lazy resolve the path to current module.
duke@0 2375 if (saved_jvm_path[0] != 0) {
duke@0 2376 strcpy(buf, saved_jvm_path);
duke@0 2377 return;
duke@0 2378 }
duke@0 2379
duke@0 2380 char dli_fname[MAXPATHLEN];
duke@0 2381 bool ret = dll_address_to_library_name(
duke@0 2382 CAST_FROM_FN_PTR(address, os::jvm_path),
duke@0 2383 dli_fname, sizeof(dli_fname), NULL);
dcubed@4930 2384 assert(ret, "cannot locate libjvm");
dcubed@4930 2385 char *rp = NULL;
dcubed@4930 2386 if (ret && dli_fname[0] != '\0') {
dcubed@4930 2387 rp = realpath(dli_fname, buf);
dcubed@4930 2388 }
bobv@1601 2389 if (rp == NULL)
xlu@513 2390 return;
duke@0 2391
sla@2149 2392 if (Arguments::created_by_gamma_launcher()) {
duke@0 2393 // Support for the gamma launcher. Typical value for buf is
duke@0 2394 // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so". If "/jre/lib/" appears at
duke@0 2395 // the right place in the string, then assume we are installed in a JDK and
duke@0 2396 // we're done. Otherwise, check for a JAVA_HOME environment variable and fix
duke@0 2397 // up the path so it looks like libjvm.so is installed there (append a
duke@0 2398 // fake suffix hotspot/libjvm.so).
duke@0 2399 const char *p = buf + strlen(buf) - 1;
duke@0 2400 for (int count = 0; p > buf && count < 5; ++count) {
duke@0 2401 for (--p; p > buf && *p != '/'; --p)
duke@0 2402 /* empty */ ;
duke@0 2403 }
duke@0 2404
duke@0 2405 if (strncmp(p, "/jre/lib/", 9) != 0) {
duke@0 2406 // Look for JAVA_HOME in the environment.
duke@0 2407 char* java_home_var = ::getenv("JAVA_HOME");
duke@0 2408 if (java_home_var != NULL && java_home_var[0] != 0) {
mchung@1562 2409 char* jrelib_p;
mchung@1562 2410 int len;
mchung@1562 2411
dcubed@3957 2412 // Check the current module name "libjvm.so".
duke@0 2413 p = strrchr(buf, '/');
duke@0 2414 assert(strstr(p, "/libjvm") == p, "invalid library name");
duke@0 2415
bobv@1601 2416 rp = realpath(java_home_var, buf);
bobv@1601 2417 if (rp == NULL)
xlu@513 2418 return;
mchung@1562 2419
mchung@1562 2420 // determine if this is a legacy image or modules image
mchung@1562 2421 // modules image doesn't have "jre" subdirectory
mchung@1562 2422 len = strlen(buf);
hseigel@6339 2423 assert(len < buflen, "Ran out of buffer room");
mchung@1562 2424 jrelib_p = buf + len;
mchung@1562 2425 snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);
mchung@1562 2426 if (0 != access(buf, F_OK)) {
mchung@1562 2427 snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);
mchung@1562 2428 }
mchung@1562 2429
duke@0 2430 if (0 == access(buf, F_OK)) {
dcubed@3957 2431 // Use current module name "libjvm.so"
mchung@1562 2432 len = strlen(buf);
dcubed@3957 2433 snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
duke@0 2434 } else {
duke@0 2435 // Go back to path of .so
bobv@1601 2436 rp = realpath(dli_fname, buf);
bobv@1601 2437 if (rp == NULL)
xlu@513 2438 return;
duke@0 2439 }
duke@0 2440 }
duke@0 2441 }
duke@0 2442 }
duke@0 2443
hseigel@6339 2444 strncpy(saved_jvm_path, buf, MAXPATHLEN);
duke@0 2445 }
duke@0 2446
duke@0 2447 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
duke@0 2448 // no prefix required, not even "_"
duke@0 2449 }
duke@0 2450
duke@0 2451 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
duke@0 2452 // no suffix required
duke@0 2453 }
duke@0 2454
duke@0 2455 ////////////////////////////////////////////////////////////////////////////////
duke@0 2456 // sun.misc.Signal support
duke@0 2457
duke@0 2458 static volatile jint sigint_count = 0;
duke@0 2459
duke@0 2460 static void
duke@0 2461 UserHandler(int sig, void *siginfo, void *context) {
duke@0 2462 // 4511530 - sem_post is serialized and handled by the manager thread. When
duke@0 2463 // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
duke@0 2464 // don't want to flood the manager thread with sem_post requests.
duke@0 2465 if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)
duke@0 2466 return;
duke@0 2467
duke@0 2468 // Ctrl-C is pressed during error reporting, likely because the error
duke@0 2469 // handler fails to abort. Let VM die immediately.
duke@0 2470 if (sig == SIGINT && is_error_reported()) {
duke@0 2471 os::die();
duke@0 2472 }
duke@0 2473
duke@0 2474 os::signal_notify(sig);
duke@0 2475 }
duke@0 2476
duke@0 2477 void* os::user_handler() {
duke@0 2478 return CAST_FROM_FN_PTR(void*, UserHandler);
duke@0 2479 }
duke@0 2480
sla@4802 2481 class Semaphore : public StackObj {
sla@4802 2482 public:
sla@4802 2483 Semaphore();
sla@4802 2484 ~Semaphore();
sla@4802 2485 void signal();
sla@4802 2486 void wait();
sla@4802 2487 bool trywait();
sla@4802 2488 bool timedwait(unsigned int sec, int nsec);
sla@4802 2489 private:
sla@4802 2490 sem_t _semaphore;
sla@4802 2491 };
sla@4802 2492
sla@4802 2493 Semaphore::Semaphore() {
sla@4802 2494 sem_init(&_semaphore, 0, 0);
sla@4802 2495 }
sla@4802 2496
sla@4802 2497 Semaphore::~Semaphore() {
sla@4802 2498 sem_destroy(&_semaphore);
sla@4802 2499 }
sla@4802 2500
sla@4802 2501 void Semaphore::signal() {
sla@4802 2502 sem_post(&_semaphore);
sla@4802 2503 }
sla@4802 2504
sla@4802 2505 void Semaphore::wait() {
sla@4802 2506 sem_wait(&_semaphore);
sla@4802 2507 }
sla@4802 2508
sla@4802 2509 bool Semaphore::trywait() {
sla@4802 2510 return sem_trywait(&_semaphore) == 0;
sla@4802 2511 }
sla@4802 2512
sla@4802 2513 bool Semaphore::timedwait(unsigned int sec, int nsec) {
mgronlun@5952 2514
sla@4802 2515 struct timespec ts;
mgronlun@5952 2516 // Semaphore's are always associated with CLOCK_REALTIME
mgronlun@5952 2517 os::Linux::clock_gettime(CLOCK_REALTIME, &ts);
mgronlun@5952 2518 // see unpackTime for discussion on overflow checking
mgronlun@5952 2519 if (sec >= MAX_SECS) {
mgronlun@5952 2520 ts.tv_sec += MAX_SECS;
mgronlun@5952 2521 ts.tv_nsec = 0;
mgronlun@5952 2522 } else {
mgronlun@5952 2523 ts.tv_sec += sec;
mgronlun@5952 2524 ts.tv_nsec += nsec;
mgronlun@5952 2525 if (ts.tv_nsec >= NANOSECS_PER_SEC) {
mgronlun@5952 2526 ts.tv_nsec -= NANOSECS_PER_SEC;
mgronlun@5952 2527 ++ts.tv_sec; // note: this must be <= max_secs
mgronlun@5952 2528 }
mgronlun@5952 2529 }
sla@4802 2530
sla@4802 2531 while (1) {
sla@4802 2532 int result = sem_timedwait(&_semaphore, &ts);
sla@4802 2533 if (result == 0) {
sla@4802 2534 return true;
sla@4802 2535 } else if (errno == EINTR) {
sla@4802 2536 continue;
sla@4802 2537 } else if (errno == ETIMEDOUT) {
sla@4802 2538 return false;
sla@4802 2539 } else {
sla@4802 2540 return false;
sla@4802 2541 }
sla@4802 2542 }
sla@4802 2543 }
sla@4802 2544
duke@0 2545 extern "C" {
duke@0 2546 typedef void (*sa_handler_t)(int);
duke@0 2547 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
duke@0 2548 }
duke@0 2549
duke@0 2550 void* os::signal(int signal_number, void* handler) {
duke@0 2551 struct sigaction sigAct, oldSigAct;
duke@0 2552
duke@0 2553 sigfillset(&(sigAct.sa_mask));
duke@0 2554 sigAct.sa_flags = SA_RESTART|SA_SIGINFO;
duke@0 2555 sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
duke@0 2556
duke@0 2557 if (sigaction(signal_number, &sigAct, &oldSigAct)) {
duke@0 2558 // -1 means registration failed
duke@0 2559 return (void *)-1;
duke@0 2560 }
duke@0 2561
duke@0 2562 return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
duke@0 2563 }
duke@0 2564
duke@0 2565 void os::signal_raise(int signal_number) {
duke@0 2566 ::raise(signal_number);
duke@0 2567 }
duke@0 2568
duke@0 2569 /*
duke@0 2570 * The following code is moved from os.cpp for making this
duke@0 2571 * code platform specific, which it is by its very nature.
duke@0 2572 */
duke@0 2573
duke@0 2574 // Will be modified when max signal is changed to be dynamic
duke@0 2575 int os::sigexitnum_pd() {
duke@0 2576 return NSIG;
duke@0 2577 }
duke@0 2578
duke@0 2579 // a counter for each possible signal value
duke@0 2580 static volatile jint pending_signals[NSIG+1] = { 0 };
duke@0 2581
duke@0 2582 // Linux(POSIX) specific hand shaking semaphore.
duke@0 2583 static sem_t sig_sem;
sla@4802 2584 static Semaphore sr_semaphore;
duke@0 2585
duke@0 2586 void os::signal_init_pd() {
duke@0 2587 // Initialize signal structures
duke@0 2588 ::memset((void*)pending_signals, 0, sizeof(pending_signals));
duke@0 2589
duke@0 2590 // Initialize signal semaphore
duke@0 2591 ::sem_init(&sig_sem, 0, 0);
duke@0 2592 }
duke@0 2593
duke@0 2594 void os::signal_notify(int sig) {
duke@0 2595 Atomic::inc(&pending_signals[sig]);
duke@0 2596 ::sem_post(&sig_sem);
duke@0 2597 }
duke@0 2598
duke@0 2599 static int check_pending_signals(bool wait) {
duke@0 2600 Atomic::store(0, &sigint_count);
duke@0 2601 for (;;) {
duke@0 2602 for (int i = 0; i < NSIG + 1; i++) {
duke@0 2603 jint n = pending_signals[i];
duke@0 2604 if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
duke@0 2605 return i;
duke@0 2606 }
duke@0 2607 }
duke@0 2608 if (!wait) {
duke@0 2609 return -1;
duke@0 2610 }
duke@0 2611 JavaThread *thread = JavaThread::current();
duke@0 2612 ThreadBlockInVM tbivm(thread);
duke@0 2613
duke@0 2614 bool threadIsSuspended;
duke@0 2615 do {
duke@0 2616 thread->set_suspend_equivalent();
duke@0 2617 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
duke@0 2618 ::sem_wait(&sig_sem);
duke@0 2619
duke@0 2620 // were we externally suspended while we were waiting?
duke@0 2621 threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
duke@0 2622 if (threadIsSuspended) {
duke@0 2623 //
duke@0 2624 // The semaphore has been incremented, but while we were waiting
duke@0 2625 // another thread suspended us. We don't want to continue running
duke@0 2626 // while suspended because that would surprise the thread that
duke@0 2627 // suspended us.
duke@0 2628 //
duke@0 2629 ::sem_post(&sig_sem);
duke@0 2630
duke@0 2631 thread->java_suspend_self();
duke@0 2632 }
duke@0 2633 } while (threadIsSuspended);
duke@0 2634 }
duke@0 2635 }
duke@0 2636
duke@0 2637 int os::signal_lookup() {
duke@0 2638 return check_pending_signals(false);
duke@0 2639 }
duke@0 2640
duke@0 2641 int os::signal_wait() {
duke@0 2642 return check_pending_signals(true);
duke@0 2643 }
duke@0 2644
duke@0 2645 ////////////////////////////////////////////////////////////////////////////////
duke@0 2646 // Virtual Memory
duke@0 2647
duke@0 2648 int os::vm_page_size() {
duke@0 2649 // Seems redundant as all get out
duke@0 2650 assert(os::Linux::page_size() != -1, "must call os::init");
duke@0 2651 return os::Linux::page_size();
duke@0 2652 }
duke@0 2653
duke@0 2654 // Solaris allocates memory by pages.
duke@0 2655 int os::vm_allocation_granularity() {
duke@0 2656 assert(os::Linux::page_size() != -1, "must call os::init");
duke@0 2657 return os::Linux::page_size();
duke@0 2658 }
duke@0 2659
duke@0 2660 // Rationale behind this function:
duke@0 2661 // current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable
duke@0 2662 // mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get
duke@0 2663 // samples for JITted code. Here we create private executable mapping over the code cache
duke@0 2664 // and then we can use standard (well, almost, as mapping can change) way to provide
duke@0 2665 // info for the reporting script by storing timestamp and location of symbol
duke@0 2666 void linux_wrap_code(char* base, size_t size) {
duke@0 2667 static volatile jint cnt = 0;
duke@0 2668
duke@0 2669 if (!UseOprofile) {
duke@0 2670 return;
duke@0 2671 }
duke@0 2672
coleenp@1417 2673 char buf[PATH_MAX+1];
duke@0 2674 int num = Atomic::add(1, &cnt);
duke@0 2675
coleenp@1353 2676 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d",
coleenp@1353 2677 os::get_temp_directory(), os::current_process_id(), num);
duke@0 2678 unlink(buf);
duke@0 2679
ikrylov@1887 2680 int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU);
duke@0 2681
duke@0 2682 if (fd != -1) {
ikrylov@1887 2683 off_t rv = ::lseek(fd, size-2, SEEK_SET);
duke@0 2684 if (rv != (off_t)-1) {
ikrylov@1887 2685 if (::write(fd, "", 1) == 1) {
duke@0 2686 mmap(base, size,
duke@0 2687 PROT_READ|PROT_WRITE|PROT_EXEC,
duke@0 2688 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0);
duke@0 2689 }
duke@0 2690 }
ikrylov@1887 2691 ::close(fd);
duke@0 2692 unlink(buf);
duke@0 2693 }
duke@0 2694 }
duke@0 2695
dcubed@4820 2696 static bool recoverable_mmap_error(int err) {
dcubed@4820 2697 // See if the error is one we can let the caller handle. This
dcubed@4820 2698 // list of errno values comes from JBS-6843484. I can't find a
dcubed@4820 2699 // Linux man page that documents this specific set of errno
dcubed@4820 2700 // values so while this list currently matches Solaris, it may
dcubed@4820 2701 // change as we gain experience with this failure mode.
dcubed@4820 2702 switch (err) {
dcubed@4820 2703 case EBADF:
dcubed@4820 2704 case EINVAL:
dcubed@4820 2705 case ENOTSUP:
dcubed@4820 2706 // let the caller deal with these errors
dcubed@4820 2707 return true;
dcubed@4820 2708
dcubed@4820 2709 default:
dcubed@4820 2710 // Any remaining errors on this OS can cause our reserved mapping
dcubed@4820 2711 // to be lost. That can cause confusion where different data
dcubed@4820 2712 // structures think they have the same memory mapped. The worst
dcubed@4820 2713 // scenario is if both the VM and a library think they have the
dcubed@4820 2714 // same memory mapped.
dcubed@4820 2715 return false;
dcubed@4820 2716 }
dcubed@4820 2717 }
dcubed@4820 2718
dcubed@4820 2719 static void warn_fail_commit_memory(char* addr, size_t size, bool exec,
dcubed@4820 2720 int err) {
dcubed@4820 2721 warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
dcubed@4820 2722 ", %d) failed; error='%s' (errno=%d)", addr, size, exec,
dcubed@4820 2723 strerror(err), err);
dcubed@4820 2724 }
dcubed@4820 2725
dcubed@4820 2726 static void warn_fail_commit_memory(char* addr, size_t size,
dcubed@4820 2727 size_t alignment_hint, bool exec,
dcubed@4820 2728 int err) {
dcubed@4820 2729 warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
dcubed@4820 2730 ", " SIZE_FORMAT ", %d) failed; error='%s' (errno=%d)", addr, size,
dcubed@4820 2731 alignment_hint, exec, strerror(err), err);
dcubed@4820 2732 }
dcubed@4820 2733
duke@0 2734 // NOTE: Linux kernel does not really reserve the pages for us.
duke@0 2735 // All it does is to check if there are enough free pages
duke@0 2736 // left at the time of mmap(). This could be a potential
duke@0 2737 // problem.
dcubed@4820 2738 int os::Linux::commit_memory_impl(char* addr, size_t size, bool exec) {
coleenp@656 2739 int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
coleenp@656 2740 uintptr_t res = (uintptr_t) ::mmap(addr, size, prot,
duke@0 2741 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);
iveresov@2650 2742 if (res != (uintptr_t) MAP_FAILED) {
iveresov@2650 2743 if (UseNUMAInterleaving) {
iveresov@2650 2744 numa_make_global(addr, size);
iveresov@2650 2745 }
dcubed@4820 2746 return 0;
dcubed@4820 2747 }
dcubed@4820 2748
dcubed@4820 2749 int err = errno; // save errno from mmap() call above
dcubed@4820 2750
dcubed@4820 2751 if (!recoverable_mmap_error(err)) {
dcubed@4820 2752 warn_fail_commit_memory(addr, size, exec, err);
dcubed@4820 2753 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory.");
dcubed@4820 2754 }
dcubed@4820 2755
dcubed@4820 2756 return err;
dcubed@4820 2757 }
dcubed@4820 2758
dcubed@4820 2759 bool os::pd_commit_memory(char* addr, size_t size, bool exec) {
dcubed@4820 2760 return os::Linux::commit_memory_impl(addr, size, exec) == 0;
dcubed@4820 2761 }
dcubed@4820 2762
dcubed@4820 2763 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
dcubed@4820 2764 const char* mesg) {
dcubed@4820 2765 assert(mesg != NULL, "mesg must be specified");
dcubed@4820 2766 int err = os::Linux::commit_memory_impl(addr, size, exec);
dcubed@4820 2767 if (err != 0) {
dcubed@4820 2768 // the caller wants all commit errors to exit with the specified mesg:
dcubed@4820 2769 warn_fail_commit_memory(addr, size, exec, err);
dcubed@4820 2770 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);
dcubed@4820 2771 }
duke@0 2772 }
duke@0 2773
iveresov@2383 2774 // Define MAP_HUGETLB here so we can build HotSpot on old systems.
iveresov@2383 2775 #ifndef MAP_HUGETLB
iveresov@2383 2776 #define MAP_HUGETLB 0x40000
iveresov@2383 2777 #endif
iveresov@2383 2778
iveresov@2383 2779 // Define MADV_HUGEPAGE here so we can build HotSpot on old systems.
iveresov@2383 2780 #ifndef MADV_HUGEPAGE
iveresov@2383 2781 #define MADV_HUGEPAGE 14
iveresov@2383 2782 #endif
iveresov@2383 2783
dcubed@4820 2784 int os::Linux::commit_memory_impl(char* addr, size_t size,
dcubed@4820 2785 size_t alignment_hint, bool exec) {
stefank@5143 2786 int err = os::Linux::commit_memory_impl(addr, size, exec);
dcubed@4820 2787 if (err == 0) {
iveresov@2678 2788 realign_memory(addr, size, alignment_hint);
dcubed@4820 2789 }
dcubed@4820 2790 return err;
dcubed@4820 2791 }
dcubed@4820 2792
dcubed@4820 2793 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,
dcubed@4820 2794 bool exec) {
dcubed@4820 2795 return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0;
dcubed@4820 2796 }
dcubed@4820 2797
dcubed@4820 2798 void os::pd_commit_memory_or_exit(char* addr, size_t size,
dcubed@4820 2799 size_t alignment_hint, bool exec,
dcubed@4820 2800 const char* mesg) {
dcubed@4820 2801 assert(mesg != NULL, "mesg must be specified");
dcubed@4820 2802 int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec);
dcubed@4820 2803 if (err != 0) {
dcubed@4820 2804 // the caller wants all commit errors to exit with the specified mesg:
dcubed@4820 2805 warn_fail_commit_memory(addr, size, alignment_hint, exec, err);
dcubed@4820 2806 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);
dcubed@4820 2807 }
duke@0 2808 }
duke@0 2809
zgu@3465 2810 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
stefank@5143 2811 if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) {
iveresov@2383 2812 // We don't check the return value: madvise(MADV_HUGEPAGE) may not
iveresov@2383 2813 // be supported or the memory may already be backed by huge pages.
iveresov@2383 2814 ::madvise(addr, bytes, MADV_HUGEPAGE);
iveresov@2383 2815 }
iveresov@2383 2816 }
iveresov@141 2817
zgu@3465 2818 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {
iveresov@3205 2819 // This method works by doing an mmap over an existing mmaping and effectively discarding
iveresov@3205 2820 // the existing pages. However it won't work for SHM-based large pages that cannot be
iveresov@3205 2821 // uncommitted at all. We don't do anything in this case to avoid creating a segment with
iveresov@3205 2822 // small pages on top of the SHM segment. This method always works for small pages, so we
iveresov@3205 2823 // allow that in any case.
stefank@5143 2824 if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) {
dcubed@4820 2825 commit_memory(addr, bytes, alignment_hint, !ExecMem);
iveresov@3205 2826 }
iveresov@141 2827 }
iveresov@141 2828
iveresov@462 2829 void os::numa_make_global(char *addr, size_t bytes) {
iveresov@462 2830 Linux::numa_interleave_memory(addr, bytes);
iveresov@462 2831 }
iveresov@141 2832
mgerdin@5209 2833 // Define for numa_set_bind_policy(int). Setting the argument to 0 will set the
mgerdin@5209 2834 // bind policy to MPOL_PREFERRED for the current thread.
mgerdin@5209 2835 #define USE_MPOL_PREFERRED 0
mgerdin@5209 2836
iveresov@141 2837 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
mgerdin@5209 2838 // To make NUMA and large pages more robust when both enabled, we need to ease
mgerdin@5209 2839 // the requirements on where the memory should be allocated. MPOL_BIND is the
mgerdin@5209 2840 // default policy and it will force memory to be allocated on the specified
mgerdin@5209 2841 // node. Changing this to MPOL_PREFERRED will prefer to allocate the memory on
mgerdin@5209 2842 // the specified node, but will not force it. Using this policy will prevent
mgerdin@5209 2843 // getting SIGBUS when trying to allocate large pages on NUMA nodes with no
mgerdin@5209 2844 // free large pages.
mgerdin@5209 2845 Linux::numa_set_bind_policy(USE_MPOL_PREFERRED);
iveresov@141 2846 Linux::numa_tonode_memory(addr, bytes, lgrp_hint);
iveresov@141 2847 }
iveresov@141 2848
iveresov@141 2849 bool os::numa_topology_changed() { return false; }
iveresov@141 2850
iveresov@141 2851 size_t os::numa_get_groups_num() {
gromero@8416 2852 // Return just the number of nodes in which it's possible to allocate memory
gromero@8416 2853 // (in numa terminology, configured nodes).
gromero@8416 2854 return Linux::numa_num_configured_nodes();
iveresov@141 2855 }
iveresov@141 2856
iveresov@141 2857 int os::numa_get_group_id() {
iveresov@141 2858 int cpu_id = Linux::sched_getcpu();
iveresov@141 2859 if (cpu_id != -1) {
iveresov@141 2860 int lgrp_id = Linux::get_node_by_cpu(cpu_id);
iveresov@141 2861 if (lgrp_id != -1) {
iveresov@141 2862 return lgrp_id;
iveresov@141 2863 }
duke@0 2864 }
duke@0 2865 return 0;
duke@0 2866 }
duke@0 2867
gromero@8416 2868 int os::Linux::get_existing_num_nodes() {
gromero@8416 2869 size_t node;
gromero@8416 2870 size_t highest_node_number = Linux::numa_max_node();
gromero@8416 2871 int num_nodes = 0;
gromero@8416 2872
gromero@8416 2873 // Get the total number of nodes in the system including nodes without memory.
gromero@8416 2874 for (node = 0; node <= highest_node_number; node++) {
gromero@8416 2875 if (isnode_in_existing_nodes(node)) {
gromero@8416 2876 num_nodes++;
gromero@8416 2877 }
gromero@8416 2878 }
gromero@8416 2879 return num_nodes;
gromero@8416 2880 }
gromero@8416 2881
iveresov@141 2882 size_t os::numa_get_leaf_groups(int *ids, size_t size) {
gromero@8416 2883 size_t highest_node_number = Linux::numa_max_node();
gromero@8416 2884 size_t i = 0;
gromero@8416 2885
gromero@8416 2886 // Map all node ids in which is possible to allocate memory. Also nodes are
gromero@8416 2887 // not always consecutively available, i.e. available from 0 to the highest
gromero@8416 2888 // node number.
gromero@8416 2889 for (size_t node = 0; node <= highest_node_number; node++) {
gromero@8416 2890 if (Linux::isnode_in_configured_nodes(node)) {
gromero@8416 2891 ids[i++] = node;
gromero@8416 2892 }
gromero@8416 2893 }
gromero@8416 2894 return i;
iveresov@141 2895 }
iveresov@141 2896
duke@0 2897 bool os::get_page_info(char *start, page_info* info) {
duke@0 2898 return false;
duke@0 2899 }
duke@0 2900
duke@0 2901 char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {
duke@0 2902 return end;
duke@0 2903 }
duke@0 2904
iveresov@2589 2905
iveresov@2589 2906 int os::Linux::sched_getcpu_syscall(void) {
csahu@7933 2907 unsigned int cpu = 0;
iveresov@2589 2908 int retval = -1;
iveresov@2589 2909
iveresov@2589 2910 #if defined(IA32)
iveresov@2652 2911 # ifndef SYS_getcpu
iveresov@2652 2912 # define SYS_getcpu 318
iveresov@2652 2913 # endif
iveresov@2589 2914 retval = syscall(SYS_getcpu, &cpu, NULL, NULL);
iveresov@2589 2915 #elif defined(AMD64)
iveresov@2652 2916 // Unfortunately we have to bring all these macros here from vsyscall.h
iveresov@2652 2917 // to be able to compile on old linuxes.
iveresov@2652 2918 # define __NR_vgetcpu 2
iveresov@2652 2919 # define VSYSCALL_START (-10UL << 20)
iveresov@2652 2920 # define VSYSCALL_SIZE 1024
iveresov@2652 2921 # define VSYSCALL_ADDR(vsyscall_nr) (VSYSCALL_START+VSYSCALL_SIZE*(vsyscall_nr))
iveresov@2589 2922 typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache);
iveresov@2589 2923 vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu);
iveresov@2589 2924 retval = vgetcpu(&cpu, NULL, NULL);
iveresov@2589 2925 #endif
iveresov@2589 2926
iveresov@2589 2927 return (retval == -1) ? retval : cpu;
iveresov@2589 2928 }
iveresov@2589 2929
coleenp@2072 2930 // Something to do with the numa-aware allocator needs these symbols
coleenp@2072 2931 extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }
coleenp@2072 2932 extern "C" JNIEXPORT void numa_error(char *where) { }
coleenp@2072 2933 extern "C" JNIEXPORT int fork1() { return fork(); }
iveresov@141 2934
zgu@8571 2935 // Handle request to load libnuma symbol version 1.1 (API v1). If it fails
zgu@8571 2936 // load symbol from base version instead.
iveresov@763 2937 void* os::Linux::libnuma_dlsym(void* handle, const char *name) {
iveresov@763 2938 void *f = dlvsym(handle, name, "libnuma_1.1");
iveresov@763 2939 if (f == NULL) {
iveresov@763 2940 f = dlsym(handle, name);
iveresov@763 2941 }
iveresov@763 2942 return f;
iveresov@763 2943 }
iveresov@763 2944
zgu@8571 2945 // Handle request to load libnuma symbol version 1.2 (API v2) only.
zgu@8571 2946 // Return NULL if the symbol is not defined in this particular version.
zgu@8571 2947 void* os::Linux::libnuma_v2_dlsym(void* handle, const char* name) {
zgu@8571 2948 return dlvsym(handle, name, "libnuma_1.2");
zgu@8571 2949 }
zgu@8571 2950
iveresov@462 2951 bool os::Linux::libnuma_init() {
iveresov@141 2952 // sched_getcpu() should be in libc.
iveresov@141 2953 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
iveresov@141 2954 dlsym(RTLD_DEFAULT, "sched_getcpu")));
iveresov@141 2955
iveresov@2589 2956 // If it's not, try a direct syscall.
iveresov@2589 2957 if (sched_getcpu() == -1)
iveresov@2589 2958 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, (void*)&sched_getcpu_syscall));
iveresov@2589 2959
iveresov@141 2960 if (sched_getcpu() != -1) { // Does it work?
iveresov@267 2961 void *handle = dlopen("libnuma.so.1", RTLD_LAZY);
iveresov@141 2962 if (handle != NULL) {
iveresov@141 2963 set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,
iveresov@763 2964 libnuma_dlsym(handle, "numa_node_to_cpus")));
iveresov@141 2965 set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,
iveresov@763 2966 libnuma_dlsym(handle, "numa_max_node")));
gromero@8416 2967 set_numa_num_configured_nodes(CAST_TO_FN_PTR(numa_num_configured_nodes_func_t,
gromero@8416 2968 libnuma_dlsym(handle, "numa_num_configured_nodes")));
iveresov@141 2969 set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,
iveresov@763 2970 libnuma_dlsym(handle, "numa_available")));
iveresov@141 2971 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,
iveresov@763 2972 libnuma_dlsym(handle, "numa_tonode_memory")));
iveresov@462 2973 set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,
gromero@8416 2974 libnuma_dlsym(handle, "numa_interleave_memory")));
zgu@8571 2975 set_numa_interleave_memory_v2(CAST_TO_FN_PTR(numa_interleave_memory_v2_func_t,
zgu@8571 2976 libnuma_v2_dlsym(handle, "numa_interleave_memory")));
mgerdin@5209 2977 set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t,
gromero@8416 2978 libnuma_dlsym(handle, "numa_set_bind_policy")));
gromero@8416 2979 set_numa_bitmask_isbitset(CAST_TO_FN_PTR(numa_bitmask_isbitset_func_t,
gromero@8416 2980 libnuma_dlsym(handle, "numa_bitmask_isbitset")));
gromero@8416 2981 set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,
gromero@8416 2982 libnuma_dlsym(handle, "numa_distance")));
iveresov@462 2983
iveresov@141 2984 if (numa_available() != -1) {
iveresov@763 2985 set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
gromero@8416 2986 set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));
gromero@8416 2987 set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));
gromero@8416 2988 // Create an index -> node mapping, since nodes are not always consecutive
gromero@8416 2989 _nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
gromero@8416 2990 rebuild_nindex_to_node_map();
iveresov@141 2991 // Create a cpu -> node mapping
zgu@3465 2992 _cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
iveresov@141 2993 rebuild_cpu_to_node_map();
iveresov@462 2994 return true;
iveresov@141 2995 }
iveresov@141 2996 }
iveresov@141 2997 }
iveresov@462 2998 return false;
iveresov@141 2999 }
iveresov@141 3000
gromero@8416 3001 void os::Linux::rebuild_nindex_to_node_map() {
gromero@8416 3002 int highest_node_number = Linux::numa_max_node();
gromero@8416 3003
gromero@8416 3004 nindex_to_node()->clear();
gromero@8416 3005 for (int node = 0; node <= highest_node_number; node++) {
gromero@8416 3006 if (Linux::isnode_in_existing_nodes(node)) {
gromero@8416 3007 nindex_to_node()->append(node);
gromero@8416 3008 }
gromero@8416 3009 }
gromero@8416 3010 }
gromero@8416 3011
iveresov@141 3012 // rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.
iveresov@141 3013 // The table is later used in get_node_by_cpu().
iveresov@141 3014 void os::Linux::rebuild_cpu_to_node_map() {
iveresov@462 3015 const size_t NCPUS = 32768; // Since the buffer size computation is very obscure
iveresov@462 3016 // in libnuma (possible values are starting from 16,
iveresov@462 3017 // and continuing up with every other power of 2, but less
iveresov@462 3018 // than the maximum number of CPUs supported by kernel), and
iveresov@462 3019 // is a subject to change (in libnuma version 2 the requirements
iveresov@462 3020 // are more reasonable) we'll just hardcode the number they use
iveresov@462 3021 // in the library.
iveresov@462 3022 const size_t BitsPerCLong = sizeof(long) * CHAR_BIT;
iveresov@462 3023
dholmes@8320 3024 size_t cpu_num = processor_count();
iveresov@462 3025 size_t cpu_map_size = NCPUS / BitsPerCLong;
iveresov@462 3026 size_t cpu_map_valid_size =
iveresov@462 3027 MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size);
iveresov@462 3028
iveresov@141 3029 cpu_to_node()->clear();
iveresov@141 3030 cpu_to_node()->at_grow(cpu_num - 1);
gromero@8416 3031
gromero@8416 3032 size_t node_num = get_existing_num_nodes();
gromero@8416 3033
gromero@8416 3034 int distance = 0;
gromero@8416 3035 int closest_distance = INT_MAX;
gromero@8416 3036 int closest_node = 0;
zgu@3465 3037 unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size, mtInternal);
iveresov@462 3038 for (size_t i = 0; i < node_num; i++) {
gromero@8416 3039 // Check if node is configured (not a memory-less node). If it is not, find
gromero@8416 3040 // the closest configured node.
gromero@8416 3041 if (!isnode_in_configured_nodes(nindex_to_node()->at(i))) {
gromero@8416 3042 closest_distance = INT_MAX;
gromero@8416 3043 // Check distance from all remaining nodes in the system. Ignore distance
gromero@8416 3044 // from itself and from another non-configured node.
gromero@8416 3045 for (size_t m = 0; m < node_num; m++) {
gromero@8416 3046 if (m != i && isnode_in_configured_nodes(nindex_to_node()->at(m))) {
gromero@8416 3047 distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));
gromero@8416 3048 // If a closest node is found, update. There is always at least one
gromero@8416 3049 // configured node in the system so there is always at least one node
gromero@8416 3050 // close.
gromero@8416 3051 if (distance != 0 && distance < closest_distance) {
gromero@8416 3052 closest_distance = distance;
gromero@8416 3053 closest_node = nindex_to_node()->at(m);
gromero@8416 3054 }
gromero@8416 3055 }
gromero@8416 3056 }
gromero@8416 3057 } else {
gromero@8416 3058 // Current node is already a configured node.
gromero@8416 3059 closest_node = nindex_to_node()->at(i);
gromero@8416 3060 }
gromero@8416 3061
gromero@8416 3062 // Get cpus from the original node and map them to the closest node. If node
gromero@8416 3063 // is a configured node (not a memory-less node), then original node and
gromero@8416 3064 // closest node are the same.
gromero@8416 3065 if (numa_node_to_cpus(nindex_to_node()->at(i), cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
iveresov@462 3066 for (size_t j = 0; j < cpu_map_valid_size; j++) {
iveresov@141 3067 if (cpu_map[j] != 0) {
iveresov@462 3068 for (size_t k = 0; k < BitsPerCLong; k++) {
iveresov@141 3069 if (cpu_map[j] & (1UL << k)) {
gromero@8416 3070 cpu_to_node()->at_put(j * BitsPerCLong + k, closest_node);
iveresov@141 3071 }
iveresov@141 3072 }
iveresov@141 3073 }
iveresov@141 3074 }
iveresov@141 3075 }
iveresov@141 3076 }
zgu@3465 3077 FREE_C_HEAP_ARRAY(unsigned long, cpu_map, mtInternal);
iveresov@141 3078 }
iveresov@141 3079
iveresov@141 3080 int os::Linux::get_node_by_cpu(int cpu_id) {
iveresov@141 3081 if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {
iveresov@141 3082 return cpu_to_node()->at(cpu_id);
iveresov@141 3083 }
iveresov@141 3084 return -1;
iveresov@141 3085 }
iveresov@141 3086
iveresov@141 3087 GrowableArray<int>* os::Linux::_cpu_to_node;
gromero@8416 3088 GrowableArray<int>* os::Linux::_nindex_to_node;
iveresov@141 3089 os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;
iveresov@141 3090 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;
iveresov@141 3091 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;
gromero@8416 3092 os::Linux::numa_num_configured_nodes_func_t os::Linux::_numa_num_configured_nodes;
iveresov@141 3093 os::Linux::numa_available_func_t os::Linux::_numa_available;
iveresov@141 3094 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;
iveresov@462 3095 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;
zgu@8571 3096 os::Linux::numa_interleave_memory_v2_func_t os::Linux::_numa_interleave_memory_v2;
mgerdin@5209 3097 os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;
gromero@8416 3098 os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;
gromero@8416 3099 os::Linux::numa_distance_func_t os::Linux::_numa_distance;
iveresov@462 3100 unsigned long* os::Linux::_numa_all_nodes;
gromero@8416 3101 struct bitmask* os::Linux::_numa_all_nodes_ptr;
gromero@8416 3102 struct bitmask* os::Linux::_numa_nodes_ptr;
iveresov@141 3103
zgu@3465 3104 bool os::pd_uncommit_memory(char* addr, size_t size) {
bobv@1601 3105 uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,
bobv@1601 3106 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
bobv@1601 3107 return res != (uintptr_t) MAP_FAILED;
duke@0 3108 }
duke@0 3109
dsamersoff@5177 3110 static
dsamersoff@5177 3111 address get_stack_commited_bottom(address bottom, size_t size) {
dsamersoff@5177 3112 address nbot = bottom;
dsamersoff@5177 3113 address ntop = bottom + size;
dsamersoff@5177 3114
dsamersoff@5177 3115 size_t page_sz = os::vm_page_size();
dsamersoff@5177 3116 unsigned pages = size / page_sz;
dsamersoff@5177 3117
dsamersoff@5177 3118 unsigned char vec[1];
dsamersoff@5177 3119 unsigned imin = 1, imax = pages + 1, imid;
dcubed@5979 3120 int mincore_return_value = 0;
dcubed@5979 3121
dcubed@5979 3122 assert(imin <= imax, "Unexpected page size");
dsamersoff@5177 3123
dsamersoff@5177 3124 while (imin < imax) {
dsamersoff@5177 3125 imid = (imax + imin) / 2;
dsamersoff@5177 3126 nbot = ntop - (imid * page_sz);
dsamersoff@5177 3127
dsamersoff@5177 3128 // Use a trick with mincore to check whether the page is mapped or not.
dsamersoff@5177 3129 // mincore sets vec to 1 if page resides in memory and to 0 if page
dsamersoff@5177 3130 // is swapped output but if page we are asking for is unmapped
dsamersoff@5177 3131 // it returns -1,ENOMEM
dsamersoff@5177 3132 mincore_return_value = mincore(nbot, page_sz, vec);
dsamersoff@5177 3133
dsamersoff@5177 3134 if (mincore_return_value == -1) {
dsamersoff@5177 3135 // Page is not mapped go up
dsamersoff@5177 3136 // to find first mapped page
dsamersoff@5177 3137 if (errno != EAGAIN) {
dsamersoff@5177 3138 assert(errno == ENOMEM, "Unexpected mincore errno");
dsamersoff@5177 3139 imax = imid;
dsamersoff@5177 3140 }
dsamersoff@5177 3141 } else {
dsamersoff@5177 3142 // Page is mapped go down
dsamersoff@5177 3143 // to find first not mapped page
dsamersoff@5177 3144 imin = imid + 1;
dsamersoff@5177 3145 }
dsamersoff@5177 3146 }
dsamersoff@5177 3147
dsamersoff@5177 3148 nbot = nbot + page_sz;
dsamersoff@5177 3149
dsamersoff@5177 3150 // Adjust stack bottom one page up if last checked page is not mapped
dsamersoff@5177 3151 if (mincore_return_value == -1) {
dsamersoff@5177 3152 nbot = nbot + page_sz;
dsamersoff@5177 3153 }
dsamersoff@5177 3154
dsamersoff@5177 3155 return nbot;
dsamersoff@5177 3156 }
dsamersoff@5177 3157
dsamersoff@5177 3158
coleenp@1320 3159 // Linux uses a growable mapping for the stack, and if the mapping for
coleenp@1320 3160 // the stack guard pages is not removed when we detach a thread the
coleenp@1320 3161 // stack cannot grow beyond the pages where the stack guard was
coleenp@1320 3162 // mapped. If at some point later in the process the stack expands to
coleenp@1320 3163 // that point, the Linux kernel cannot expand the stack any further
coleenp@1320 3164 // because the guard pages are in the way, and a segfault occurs.
coleenp@1320 3165 //
coleenp@1320 3166 // However, it's essential not to split the stack region by unmapping
coleenp@1320 3167 // a region (leaving a hole) that's already part of the stack mapping,
coleenp@1320 3168 // so if the stack mapping has already grown beyond the guard pages at
coleenp@1320 3169 // the time we create them, we have to truncate the stack mapping.
coleenp@1320 3170 // So, we need to know the extent of the stack mapping when
coleenp@1320 3171 // create_stack_guard_pages() is called.
coleenp@1320 3172
coleenp@1320 3173 // We only need this for stacks that are growable: at the time of
coleenp@1320 3174 // writing thread stacks don't use growable mappings (i.e. those
coleenp@1320 3175 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
coleenp@1320 3176 // only applies to the main thread.
dsamersoff@2316 3177
coleenp@1320 3178 // If the (growable) stack mapping already extends beyond the point
coleenp@1320 3179 // where we're going to put our guard pages, truncate the mapping at
coleenp@1320 3180 // that point by munmap()ping it. This ensures that when we later
coleenp@1320 3181 // munmap() the guard pages we don't leave a hole in the stack
dbuck@8815 3182 // mapping. This only affects the main/primordial thread
dsamersoff@5177 3183
zgu@3465 3184 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
dsamersoff@5177 3185
dbuck@8815 3186 if (os::is_primordial_thread()) {
dsamersoff@5177 3187 // As we manually grow stack up to bottom inside create_attached_thread(),
dsamersoff@5177 3188 // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
dsamersoff@5177 3189 // we don't need to do anything special.
dsamersoff@5177 3190 // Check it first, before calling heavy function.
dsamersoff@5177 3191 uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
dsamersoff@5177 3192 unsigned char vec[1];
dsamersoff@5177 3193
dsamersoff@5177 3194 if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {
dsamersoff@5177 3195 // Fallback to slow path on all errors, including EAGAIN
dsamersoff@5177 3196 stack_extent = (uintptr_t) get_stack_commited_bottom(
dsamersoff@5177 3197 os::Linux::initial_thread_stack_bottom(),
dsamersoff@5177 3198 (size_t)addr - stack_extent);
dsamersoff@5177 3199 }
dsamersoff@5177 3200
dsamersoff@5177 3201 if (stack_extent < (uintptr_t)addr) {
dsamersoff@5177 3202 ::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));
dsamersoff@5177 3203 }
coleenp@1320 3204 }
coleenp@1320 3205
dcubed@4820 3206 return os::commit_memory(addr, size, !ExecMem);
coleenp@1320 3207 }
coleenp@1320 3208
coleenp@1320 3209 // If this is a growable mapping, remove the guard pages entirely by
dholmes@1670 3210 // munmap()ping them. If not, just call uncommit_memory(). This only
dbuck@8815 3211 // affects the main/primordial thread, but guard against future OS changes.
dbuck@8815 3212 // It's safe to always unmap guard pages for primordial thread because we
dbuck@8815 3213 // always place it right after end of the mapped region.
dsamersoff@5177 3214
coleenp@1320 3215 bool os::remove_stack_guard_pages(char* addr, size_t size) {
coleenp@1320 3216 uintptr_t stack_extent, stack_base;
dsamersoff@5177 3217
dbuck@8815 3218 if (os::is_primordial_thread()) {
coleenp@1320 3219 return ::munmap(addr, size) == 0;
coleenp@1320 3220 }
coleenp@1320 3221
coleenp@1320 3222 return os::uncommit_memory(addr, size);
coleenp@1320 3223 }
coleenp@1320 3224
duke@0 3225 static address _highest_vm_reserved_address = NULL;
duke@0 3226
duke@0 3227 // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory
duke@0 3228 // at 'requested_addr'. If there are existing memory mappings at the same
duke@0 3229 // location, however, they will be overwritten. If 'fixed' is false,
duke@0 3230 // 'requested_addr' is only treated as a hint, the return value may or
duke@0 3231 // may not start from the requested address. Unlike Linux mmap(), this
duke@0 3232 // function returns NULL to indicate failure.
duke@0 3233 static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {
duke@0 3234 char * addr;
duke@0 3235 int flags;
duke@0 3236
duke@0 3237 flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;
duke@0 3238 if (fixed) {
duke@0 3239 assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address");
duke@0 3240 flags |= MAP_FIXED;
duke@0 3241 }
duke@0 3242
mikael@4554 3243 // Map reserved/uncommitted pages PROT_NONE so we fail early if we
mikael@4554 3244 // touch an uncommitted page. Otherwise, the read/write might
mikael@4554 3245 // succeed if we have enough swap space to back the physical page.
mikael@4554 3246 addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
duke@0 3247 flags, -1, 0);
duke@0 3248
duke@0 3249 if (addr != MAP_FAILED) {
duke@0 3250 // anon_mmap() should only get called during VM initialization,
duke@0 3251 // don't need lock (actually we can skip locking even it can be called
duke@0 3252 // from multiple threads, because _highest_vm_reserved_address is just a
duke@0 3253 // hint about the upper limit of non-stack memory regions.)
duke@0 3254 if ((address)addr + bytes > _highest_vm_reserved_address) {
duke@0 3255 _highest_vm_reserved_address = (address)addr + bytes;
duke@0 3256 }
duke@0 3257 }
duke@0 3258
duke@0 3259 return addr == MAP_FAILED ? NULL : addr;
duke@0 3260 }
duke@0 3261
stuefe@8083 3262 // Allocate (using mmap, NO_RESERVE, with small pages) at either a given request address
stuefe@8083 3263 // (req_addr != NULL) or with a given alignment.
stuefe@8083 3264 // - bytes shall be a multiple of alignment.
stuefe@8083 3265 // - req_addr can be NULL. If not NULL, it must be a multiple of alignment.
stuefe@8083 3266 // - alignment sets the alignment at which memory shall be allocated.
stuefe@8083 3267 // It must be a multiple of allocation granularity.
stuefe@8083 3268 // Returns address of memory or NULL. If req_addr was not NULL, will only return
stuefe@8083 3269 // req_addr or NULL.
stuefe@8083 3270 static char* anon_mmap_aligned(size_t bytes, size_t alignment, char* req_addr) {
stuefe@8083 3271
stuefe@8083 3272 size_t extra_size = bytes;
stuefe@8083 3273 if (req_addr == NULL && alignment > 0) {
stuefe@8083 3274 extra_size += alignment;
stuefe@8083 3275 }
stuefe@8083 3276
stuefe@8083 3277 char* start = (char*) ::mmap(req_addr, extra_size, PROT_NONE,
stuefe@8083 3278 MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,
stuefe@8083 3279 -1, 0);
stuefe@8083 3280 if (start == MAP_FAILED) {
stuefe@8083 3281 start = NULL;
stuefe@8083 3282 } else {
stuefe@8083 3283 if (req_addr != NULL) {
stuefe@8083 3284 if (start != req_addr) {
stuefe@8083 3285 ::munmap(start, extra_size);
stuefe@8083 3286 start = NULL;
stuefe@8083 3287 }
stuefe@8083 3288 } else {
stuefe@8083 3289 char* const start_aligned = (char*) align_ptr_up(start, alignment);
stuefe@8083 3290 char* const end_aligned = start_aligned + bytes;
stuefe@8083 3291 char* const end = start + extra_size;
stuefe@8083 3292 if (start_aligned > start) {
stuefe@8083 3293 ::munmap(start, start_aligned - start);
stuefe@8083 3294 }
stuefe@8083 3295 if (end_aligned < end) {
stuefe@8083 3296 ::munmap(end_aligned, end - end_aligned);
stuefe@8083 3297 }
stuefe@8083 3298 start = start_aligned;
stuefe@8083 3299 }
stuefe@8083 3300 }
stuefe@8083 3301 return start;
stefank@8084 3302 }
stefank@8084 3303
stefank@8084 3304 // Don't update _highest_vm_reserved_address, because there might be memory
stefank@8084 3305 // regions above addr + size. If so, releasing a memory region only creates
stefank@8084 3306 // a hole in the address space, it doesn't help prevent heap-stack collision.
stefank@8084 3307 //
stefank@8084 3308 static int anon_munmap(char * addr, size_t size) {
stefank@8084 3309 return ::munmap(addr, size) == 0;
stefank@8084 3310 }
stefank@8084 3311
stefank@8084 3312 char* os::pd_reserve_memory(size_t bytes, char* requested_addr,
stefank@8084 3313 size_t alignment_hint) {
stefank@8084 3314 return anon_mmap(requested_addr, bytes, (requested_addr != NULL));
stefank@8084 3315 }
stefank@8084 3316
stefank@8084 3317 bool os::pd_release_memory(char* addr, size_t size) {
stefank@8084 3318 return anon_munmap(addr, size);
stefank@8084 3319 }
stefank@8084 3320
stefank@8084 3321 static address highest_vm_reserved_address() {
stefank@8084 3322 return _highest_vm_reserved_address;
stefank@8084 3323 }
stefank@8084 3324
stefank@8084 3325 static bool linux_mprotect(char* addr, size_t size, int prot) {
stefank@8084 3326 // Linux wants the mprotect address argument to be page aligned.
stefank@8084 3327 char* bottom = (char*)align_size_down((intptr_t)addr, os::Linux::page_size());
stefank@8084 3328
stefank@8084 3329 // According to SUSv3, mprotect() should only be used with mappings
stefank@8084 3330 // established by mmap(), and mmap() always maps whole pages. Unaligned
stefank@8084 3331 // 'addr' likely indicates problem in the VM (e.g. trying to change
stefank@8084 3332 // protection of malloc'ed or statically allocated memory). Check the
stefank@8084 3333 // caller if you hit this assert.
stefank@8084 3334 assert(addr == bottom, "sanity check");
stefank@8084 3335
stefank@8084 3336 size = align_size_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());
stefank@8084 3337 return ::mprotect(bottom, size, prot) == 0;
stefank@8084 3338 }
stefank@8084 3339
stefank@8084 3340 // Set protections specified
stefank@8084 3341 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
stefank@8084 3342 bool is_committed) {
stefank@8084 3343 unsigned int p = 0;
stefank@8084 3344 switch (prot) {
stefank@8084 3345 case MEM_PROT_NONE: p = PROT_NONE; break;
stefank@8084 3346 case MEM_PROT_READ: p = PROT_READ; break;
stefank@8084 3347 case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break;
stefank@8084 3348 case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break;
stefank@8084 3349 default:
stefank@8084 3350 ShouldNotReachHere();
stefank@8084 3351 }
stefank@8084 3352 // is_committed is unused.
stefank@8084 3353 return linux_mprotect(addr, bytes, p);
stefank@8084 3354 }
stefank@8084 3355
stefank@8084 3356 bool os::guard_memory(char* addr, size_t size) {
stefank@8084 3357 return linux_mprotect(addr, size, PROT_NONE);
stefank@8084 3358 }
stefank@8084 3359
stefank@8084 3360 bool os::unguard_memory(char* addr, size_t size) {
stefank@8084 3361 return linux_mprotect(addr, size, PROT_READ|PROT_WRITE);
stefank@8084 3362 }
stefank@8084 3363
stefank@8084 3364 bool os::Linux::transparent_huge_pages_sanity_check(bool warn, size_t page_size) {
stefank@8084 3365 bool result = false;
stefank@8084 3366 void *p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,