annotate src/os/bsd/vm/os_bsd.cpp @ 3055:436b4a3231bf

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