1 /* 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2012, 2014 SAP AG. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 // According to the AIX OS doc #pragma alloca must be used 27 // with C++ compiler before referencing the function alloca() 28 #pragma alloca 29 30 // no precompiled headers 31 #include "classfile/classLoader.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/vmSymbols.hpp" 34 #include "code/icBuffer.hpp" 35 #include "code/vtableStubs.hpp" 36 #include "compiler/compileBroker.hpp" 37 #include "interpreter/interpreter.hpp" 38 #include "jvm_aix.h" 39 #include "libperfstat_aix.hpp" 40 #include "loadlib_aix.hpp" 41 #include "memory/allocation.inline.hpp" 42 #include "memory/filemap.hpp" 43 #include "mutex_aix.inline.hpp" 44 #include "oops/oop.inline.hpp" 45 #include "os_aix.inline.hpp" 46 #include "os_share_aix.hpp" 47 #include "porting_aix.hpp" 48 #include "prims/jniFastGetField.hpp" 49 #include "prims/jvm.h" 50 #include "prims/jvm_misc.hpp" 51 #include "runtime/arguments.hpp" 52 #include "runtime/atomic.inline.hpp" 53 #include "runtime/extendedPC.hpp" 54 #include "runtime/globals.hpp" 55 #include "runtime/interfaceSupport.hpp" 56 #include "runtime/java.hpp" 57 #include "runtime/javaCalls.hpp" 58 #include "runtime/mutexLocker.hpp" 59 #include "runtime/objectMonitor.hpp" 60 #include "runtime/orderAccess.inline.hpp" 61 #include "runtime/os.hpp" 62 #include "runtime/osThread.hpp" 63 #include "runtime/perfMemory.hpp" 64 #include "runtime/sharedRuntime.hpp" 65 #include "runtime/statSampler.hpp" 66 #include "runtime/stubRoutines.hpp" 67 #include "runtime/thread.inline.hpp" 68 #include "runtime/threadCritical.hpp" 69 #include "runtime/timer.hpp" 70 #include "runtime/vm_version.hpp" 71 #include "services/attachListener.hpp" 72 #include "services/runtimeService.hpp" 73 #include "utilities/decoder.hpp" 74 #include "utilities/defaultStream.hpp" 75 #include "utilities/events.hpp" 76 #include "utilities/growableArray.hpp" 77 #include "utilities/vmError.hpp" 78 79 // put OS-includes here (sorted alphabetically) 80 #include <errno.h> 81 #include <fcntl.h> 82 #include <inttypes.h> 83 #include <poll.h> 84 #include <procinfo.h> 85 #include <pthread.h> 86 #include <pwd.h> 87 #include <semaphore.h> 88 #include <signal.h> 89 #include <stdint.h> 90 #include <stdio.h> 91 #include <string.h> 92 #include <unistd.h> 93 #include <sys/ioctl.h> 94 #include <sys/ipc.h> 95 #include <sys/mman.h> 96 #include <sys/resource.h> 97 #include <sys/select.h> 98 #include <sys/shm.h> 99 #include <sys/socket.h> 100 #include <sys/stat.h> 101 #include <sys/sysinfo.h> 102 #include <sys/systemcfg.h> 103 #include <sys/time.h> 104 #include <sys/times.h> 105 #include <sys/types.h> 106 #include <sys/utsname.h> 107 #include <sys/vminfo.h> 108 #include <sys/wait.h> 109 110 // If RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling 111 // getrusage() is prepared to handle the associated failure. 112 #ifndef RUSAGE_THREAD 113 #define RUSAGE_THREAD (1) /* only the calling thread */ 114 #endif 115 116 // Add missing declarations (should be in procinfo.h but isn't until AIX 6.1). 117 #if !defined(_AIXVERSION_610) 118 extern "C" { 119 int getthrds64(pid_t ProcessIdentifier, 120 struct thrdentry64* ThreadBuffer, 121 int ThreadSize, 122 tid64_t* IndexPointer, 123 int Count); 124 } 125 #endif 126 127 #define MAX_PATH (2 * K) 128 129 // for timer info max values which include all bits 130 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 131 // for multipage initialization error analysis (in 'g_multipage_error') 132 #define ERROR_MP_OS_TOO_OLD 100 133 #define ERROR_MP_EXTSHM_ACTIVE 101 134 #define ERROR_MP_VMGETINFO_FAILED 102 135 #define ERROR_MP_VMGETINFO_CLAIMS_NO_SUPPORT_FOR_64K 103 136 137 // The semantics in this file are thus that codeptr_t is a *real code ptr*. 138 // This means that any function taking codeptr_t as arguments will assume 139 // a real codeptr and won't handle function descriptors (eg getFuncName), 140 // whereas functions taking address as args will deal with function 141 // descriptors (eg os::dll_address_to_library_name). 142 typedef unsigned int* codeptr_t; 143 144 // Typedefs for stackslots, stack pointers, pointers to op codes. 145 typedef unsigned long stackslot_t; 146 typedef stackslot_t* stackptr_t; 147 148 // Excerpts from systemcfg.h definitions newer than AIX 5.3. 149 #ifndef PV_7 150 #define PV_7 0x200000 /* Power PC 7 */ 151 #define PV_7_Compat 0x208000 /* Power PC 7 */ 152 #endif 153 #ifndef PV_8 154 #define PV_8 0x300000 /* Power PC 8 */ 155 #define PV_8_Compat 0x308000 /* Power PC 8 */ 156 #endif 157 158 #define trcVerbose(fmt, ...) { /* PPC port */ \ 159 if (Verbose) { \ 160 fprintf(stderr, fmt, ##__VA_ARGS__); \ 161 fputc('\n', stderr); fflush(stderr); \ 162 } \ 163 } 164 #define trc(fmt, ...) /* PPC port */ 165 166 #define ERRBYE(s) { \ 167 trcVerbose(s); \ 168 return -1; \ 169 } 170 171 // query dimensions of the stack of the calling thread 172 static void query_stack_dimensions(address* p_stack_base, size_t* p_stack_size); 173 174 // function to check a given stack pointer against given stack limits 175 inline bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) { 176 if (((uintptr_t)sp) & 0x7) { 177 return false; 178 } 179 if (sp > stack_base) { 180 return false; 181 } 182 if (sp < (stackptr_t) ((address)stack_base - stack_size)) { 183 return false; 184 } 185 return true; 186 } 187 188 // returns true if function is a valid codepointer 189 inline bool is_valid_codepointer(codeptr_t p) { 190 if (!p) { 191 return false; 192 } 193 if (((uintptr_t)p) & 0x3) { 194 return false; 195 } 196 if (LoadedLibraries::find_for_text_address((address)p) == NULL) { 197 return false; 198 } 199 return true; 200 } 201 202 // Macro to check a given stack pointer against given stack limits and to die if test fails. 203 #define CHECK_STACK_PTR(sp, stack_base, stack_size) { \ 204 guarantee(is_valid_stackpointer((stackptr_t)(sp), (stackptr_t)(stack_base), stack_size), "Stack Pointer Invalid"); \ 205 } 206 207 // Macro to check the current stack pointer against given stacklimits. 208 #define CHECK_CURRENT_STACK_PTR(stack_base, stack_size) { \ 209 address sp; \ 210 sp = os::current_stack_pointer(); \ 211 CHECK_STACK_PTR(sp, stack_base, stack_size); \ 212 } 213 214 //////////////////////////////////////////////////////////////////////////////// 215 // global variables (for a description see os_aix.hpp) 216 217 julong os::Aix::_physical_memory = 0; 218 pthread_t os::Aix::_main_thread = ((pthread_t)0); 219 int os::Aix::_page_size = -1; 220 int os::Aix::_on_pase = -1; 221 int os::Aix::_os_version = -1; 222 int os::Aix::_stack_page_size = -1; 223 size_t os::Aix::_shm_default_page_size = -1; 224 int os::Aix::_can_use_64K_pages = -1; 225 int os::Aix::_can_use_16M_pages = -1; 226 int os::Aix::_xpg_sus_mode = -1; 227 int os::Aix::_extshm = -1; 228 int os::Aix::_logical_cpus = -1; 229 230 //////////////////////////////////////////////////////////////////////////////// 231 // local variables 232 233 static int g_multipage_error = -1; // error analysis for multipage initialization 234 static jlong initial_time_count = 0; 235 static int clock_tics_per_sec = 100; 236 static sigset_t check_signal_done; // For diagnostics to print a message once (see run_periodic_checks) 237 static bool check_signals = true; 238 static pid_t _initial_pid = 0; 239 static int SR_signum = SIGUSR2; // Signal used to suspend/resume a thread (must be > SIGSEGV, see 4355769) 240 static sigset_t SR_sigset; 241 static pthread_mutex_t dl_mutex; // Used to protect dlsym() calls. 242 243 julong os::available_memory() { 244 return Aix::available_memory(); 245 } 246 247 julong os::Aix::available_memory() { 248 os::Aix::meminfo_t mi; 249 if (os::Aix::get_meminfo(&mi)) { 250 return mi.real_free; 251 } else { 252 return 0xFFFFFFFFFFFFFFFFLL; 253 } 254 } 255 256 julong os::physical_memory() { 257 return Aix::physical_memory(); 258 } 259 260 //////////////////////////////////////////////////////////////////////////////// 261 // environment support 262 263 bool os::getenv(const char* name, char* buf, int len) { 264 const char* val = ::getenv(name); 265 if (val != NULL && strlen(val) < (size_t)len) { 266 strcpy(buf, val); 267 return true; 268 } 269 if (len > 0) buf[0] = 0; // return a null string 270 return false; 271 } 272 273 // Return true if user is running as root. 274 275 bool os::have_special_privileges() { 276 static bool init = false; 277 static bool privileges = false; 278 if (!init) { 279 privileges = (getuid() != geteuid()) || (getgid() != getegid()); 280 init = true; 281 } 282 return privileges; 283 } 284 285 // Helper function, emulates disclaim64 using multiple 32bit disclaims 286 // because we cannot use disclaim64() on AS/400 and old AIX releases. 287 static bool my_disclaim64(char* addr, size_t size) { 288 289 if (size == 0) { 290 return true; 291 } 292 293 // Maximum size 32bit disclaim() accepts. (Theoretically 4GB, but I just do not trust that.) 294 const unsigned int maxDisclaimSize = 0x80000000; 295 296 const unsigned int numFullDisclaimsNeeded = (size / maxDisclaimSize); 297 const unsigned int lastDisclaimSize = (size % maxDisclaimSize); 298 299 char* p = addr; 300 301 for (int i = 0; i < numFullDisclaimsNeeded; i ++) { 302 if (::disclaim(p, maxDisclaimSize, DISCLAIM_ZEROMEM) != 0) { 303 trc("Cannot disclaim %p - %p (errno %d)\n", p, p + maxDisclaimSize, errno); 304 return false; 305 } 306 p += maxDisclaimSize; 307 } 308 309 if (lastDisclaimSize > 0) { 310 if (::disclaim(p, lastDisclaimSize, DISCLAIM_ZEROMEM) != 0) { 311 trc("Cannot disclaim %p - %p (errno %d)\n", p, p + lastDisclaimSize, errno); 312 return false; 313 } 314 } 315 316 return true; 317 } 318 319 // Cpu architecture string 320 #if defined(PPC32) 321 static char cpu_arch[] = "ppc"; 322 #elif defined(PPC64) 323 static char cpu_arch[] = "ppc64"; 324 #else 325 #error Add appropriate cpu_arch setting 326 #endif 327 328 329 // Given an address, returns the size of the page backing that address. 330 size_t os::Aix::query_pagesize(void* addr) { 331 332 vm_page_info pi; 333 pi.addr = (uint64_t)addr; 334 if (::vmgetinfo(&pi, VM_PAGE_INFO, sizeof(pi)) == 0) { 335 return pi.pagesize; 336 } else { 337 fprintf(stderr, "vmgetinfo failed to retrieve page size for address %p (errno %d).\n", addr, errno); 338 assert(false, "vmgetinfo failed to retrieve page size"); 339 return SIZE_4K; 340 } 341 342 } 343 344 // Returns the kernel thread id of the currently running thread. 345 pid_t os::Aix::gettid() { 346 return (pid_t) thread_self(); 347 } 348 349 void os::Aix::initialize_system_info() { 350 351 // Get the number of online(logical) cpus instead of configured. 352 os::_processor_count = sysconf(_SC_NPROCESSORS_ONLN); 353 assert(_processor_count > 0, "_processor_count must be > 0"); 354 355 // Retrieve total physical storage. 356 os::Aix::meminfo_t mi; 357 if (!os::Aix::get_meminfo(&mi)) { 358 fprintf(stderr, "os::Aix::get_meminfo failed.\n"); fflush(stderr); 359 assert(false, "os::Aix::get_meminfo failed."); 360 } 361 _physical_memory = (julong) mi.real_total; 362 } 363 364 // Helper function for tracing page sizes. 365 static const char* describe_pagesize(size_t pagesize) { 366 switch (pagesize) { 367 case SIZE_4K : return "4K"; 368 case SIZE_64K: return "64K"; 369 case SIZE_16M: return "16M"; 370 case SIZE_16G: return "16G"; 371 default: 372 assert(false, "surprise"); 373 return "??"; 374 } 375 } 376 377 // Retrieve information about multipage size support. Will initialize 378 // Aix::_page_size, Aix::_stack_page_size, Aix::_can_use_64K_pages, 379 // Aix::_can_use_16M_pages. 380 // Must be called before calling os::large_page_init(). 381 void os::Aix::query_multipage_support() { 382 383 guarantee(_page_size == -1 && 384 _stack_page_size == -1 && 385 _can_use_64K_pages == -1 && 386 _can_use_16M_pages == -1 && 387 g_multipage_error == -1, 388 "do not call twice"); 389 390 _page_size = ::sysconf(_SC_PAGESIZE); 391 392 // This really would surprise me. 393 assert(_page_size == SIZE_4K, "surprise!"); 394 395 396 // Query default data page size (default page size for C-Heap, pthread stacks and .bss). 397 // Default data page size is influenced either by linker options (-bdatapsize) 398 // or by environment variable LDR_CNTRL (suboption DATAPSIZE). If none is given, 399 // default should be 4K. 400 size_t data_page_size = SIZE_4K; 401 { 402 void* p = os::malloc(SIZE_16M, mtInternal); 403 guarantee(p != NULL, "malloc failed"); 404 data_page_size = os::Aix::query_pagesize(p); 405 os::free(p); 406 } 407 408 // query default shm page size (LDR_CNTRL SHMPSIZE) 409 { 410 const int shmid = ::shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR | S_IWUSR); 411 guarantee(shmid != -1, "shmget failed"); 412 void* p = ::shmat(shmid, NULL, 0); 413 ::shmctl(shmid, IPC_RMID, NULL); 414 guarantee(p != (void*) -1, "shmat failed"); 415 _shm_default_page_size = os::Aix::query_pagesize(p); 416 ::shmdt(p); 417 } 418 419 // before querying the stack page size, make sure we are not running as primordial 420 // thread (because primordial thread's stack may have different page size than 421 // pthread thread stacks). Running a VM on the primordial thread won't work for a 422 // number of reasons so we may just as well guarantee it here 423 guarantee(!os::Aix::is_primordial_thread(), "Must not be called for primordial thread"); 424 425 // query stack page size 426 { 427 int dummy = 0; 428 _stack_page_size = os::Aix::query_pagesize(&dummy); 429 // everything else would surprise me and should be looked into 430 guarantee(_stack_page_size == SIZE_4K || _stack_page_size == SIZE_64K, "Wrong page size"); 431 // also, just for completeness: pthread stacks are allocated from C heap, so 432 // stack page size should be the same as data page size 433 guarantee(_stack_page_size == data_page_size, "stack page size should be the same as data page size"); 434 } 435 436 // EXTSHM is bad: among other things, it prevents setting pagesize dynamically 437 // for system V shm. 438 if (Aix::extshm()) { 439 if (Verbose) { 440 fprintf(stderr, "EXTSHM is active - will disable large page support.\n" 441 "Please make sure EXTSHM is OFF for large page support.\n"); 442 } 443 g_multipage_error = ERROR_MP_EXTSHM_ACTIVE; 444 _can_use_64K_pages = _can_use_16M_pages = 0; 445 goto query_multipage_support_end; 446 } 447 448 // now check which page sizes the OS claims it supports, and of those, which actually can be used. 449 { 450 const int MAX_PAGE_SIZES = 4; 451 psize_t sizes[MAX_PAGE_SIZES]; 452 const int num_psizes = ::vmgetinfo(sizes, VMINFO_GETPSIZES, MAX_PAGE_SIZES); 453 if (num_psizes == -1) { 454 if (Verbose) { 455 fprintf(stderr, "vmgetinfo(VMINFO_GETPSIZES) failed (errno: %d)\n", errno); 456 fprintf(stderr, "disabling multipage support.\n"); 457 } 458 g_multipage_error = ERROR_MP_VMGETINFO_FAILED; 459 _can_use_64K_pages = _can_use_16M_pages = 0; 460 goto query_multipage_support_end; 461 } 462 guarantee(num_psizes > 0, "vmgetinfo(.., VMINFO_GETPSIZES, ...) failed."); 463 assert(num_psizes <= MAX_PAGE_SIZES, "Surprise! more than 4 page sizes?"); 464 if (Verbose) { 465 fprintf(stderr, "vmgetinfo(.., VMINFO_GETPSIZES, ...) returns %d supported page sizes: ", num_psizes); 466 for (int i = 0; i < num_psizes; i ++) { 467 fprintf(stderr, " %s ", describe_pagesize(sizes[i])); 468 } 469 fprintf(stderr, " .\n"); 470 } 471 472 // Can we use 64K, 16M pages? 473 _can_use_64K_pages = 0; 474 _can_use_16M_pages = 0; 475 for (int i = 0; i < num_psizes; i ++) { 476 if (sizes[i] == SIZE_64K) { 477 _can_use_64K_pages = 1; 478 } else if (sizes[i] == SIZE_16M) { 479 _can_use_16M_pages = 1; 480 } 481 } 482 483 if (!_can_use_64K_pages) { 484 g_multipage_error = ERROR_MP_VMGETINFO_CLAIMS_NO_SUPPORT_FOR_64K; 485 } 486 487 // Double-check for 16M pages: Even if AIX claims to be able to use 16M pages, 488 // there must be an actual 16M page pool, and we must run with enough rights. 489 if (_can_use_16M_pages) { 490 const int shmid = ::shmget(IPC_PRIVATE, SIZE_16M, IPC_CREAT | S_IRUSR | S_IWUSR); 491 guarantee(shmid != -1, "shmget failed"); 492 struct shmid_ds shm_buf = { 0 }; 493 shm_buf.shm_pagesize = SIZE_16M; 494 const bool can_set_pagesize = ::shmctl(shmid, SHM_PAGESIZE, &shm_buf) == 0 ? true : false; 495 const int en = errno; 496 ::shmctl(shmid, IPC_RMID, NULL); 497 if (!can_set_pagesize) { 498 if (Verbose) { 499 fprintf(stderr, "Failed to allocate even one misely 16M page. shmctl failed with %d (%s).\n" 500 "Will deactivate 16M support.\n", en, strerror(en)); 501 } 502 _can_use_16M_pages = 0; 503 } 504 } 505 506 } // end: check which pages can be used for shared memory 507 508 query_multipage_support_end: 509 510 guarantee(_page_size != -1 && 511 _stack_page_size != -1 && 512 _can_use_64K_pages != -1 && 513 _can_use_16M_pages != -1, "Page sizes not properly initialized"); 514 515 if (_can_use_64K_pages) { 516 g_multipage_error = 0; 517 } 518 519 if (Verbose) { 520 fprintf(stderr, "Data page size (C-Heap, bss, etc): %s\n", describe_pagesize(data_page_size)); 521 fprintf(stderr, "Thread stack page size (pthread): %s\n", describe_pagesize(_stack_page_size)); 522 fprintf(stderr, "Default shared memory page size: %s\n", describe_pagesize(_shm_default_page_size)); 523 fprintf(stderr, "Can use 64K pages dynamically with shared meory: %s\n", (_can_use_64K_pages ? "yes" :"no")); 524 fprintf(stderr, "Can use 16M pages dynamically with shared memory: %s\n", (_can_use_16M_pages ? "yes" :"no")); 525 fprintf(stderr, "Multipage error details: %d\n", g_multipage_error); 526 } 527 528 } // end os::Aix::query_multipage_support() 529 530 void os::init_system_properties_values() { 531 532 #define DEFAULT_LIBPATH "/usr/lib:/lib" 533 #define EXTENSIONS_DIR "/lib/ext" 534 535 // Buffer that fits several sprintfs. 536 // Note that the space for the trailing null is provided 537 // by the nulls included by the sizeof operator. 538 const size_t bufsize = 539 MAX2((size_t)MAXPATHLEN, // For dll_dir & friends. 540 (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR)); // extensions dir 541 char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); 542 543 // sysclasspath, java_home, dll_dir 544 { 545 char *pslash; 546 os::jvm_path(buf, bufsize); 547 548 // Found the full path to libjvm.so. 549 // Now cut the path to <java_home>/jre if we can. 550 *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so. 551 pslash = strrchr(buf, '/'); 552 if (pslash != NULL) { 553 *pslash = '\0'; // Get rid of /{client|server|hotspot}. 554 } 555 Arguments::set_dll_dir(buf); 556 557 if (pslash != NULL) { 558 pslash = strrchr(buf, '/'); 559 if (pslash != NULL) { 560 *pslash = '\0'; // Get rid of /<arch>. 561 pslash = strrchr(buf, '/'); 562 if (pslash != NULL) { 563 *pslash = '\0'; // Get rid of /lib. 564 } 565 } 566 } 567 Arguments::set_java_home(buf); 568 set_boot_path('/', ':'); 569 } 570 571 // Where to look for native libraries. 572 573 // On Aix we get the user setting of LIBPATH. 574 // Eventually, all the library path setting will be done here. 575 // Get the user setting of LIBPATH. 576 const char *v = ::getenv("LIBPATH"); 577 const char *v_colon = ":"; 578 if (v == NULL) { v = ""; v_colon = ""; } 579 580 // Concatenate user and invariant part of ld_library_path. 581 // That's +1 for the colon and +1 for the trailing '\0'. 582 char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + sizeof(DEFAULT_LIBPATH) + 1, mtInternal); 583 sprintf(ld_library_path, "%s%s" DEFAULT_LIBPATH, v, v_colon); 584 Arguments::set_library_path(ld_library_path); 585 FREE_C_HEAP_ARRAY(char, ld_library_path); 586 587 // Extensions directories. 588 sprintf(buf, "%s" EXTENSIONS_DIR, Arguments::get_java_home()); 589 Arguments::set_ext_dirs(buf); 590 591 FREE_C_HEAP_ARRAY(char, buf); 592 593 #undef DEFAULT_LIBPATH 594 #undef EXTENSIONS_DIR 595 } 596 597 //////////////////////////////////////////////////////////////////////////////// 598 // breakpoint support 599 600 void os::breakpoint() { 601 BREAKPOINT; 602 } 603 604 extern "C" void breakpoint() { 605 // use debugger to set breakpoint here 606 } 607 608 //////////////////////////////////////////////////////////////////////////////// 609 // signal support 610 611 debug_only(static bool signal_sets_initialized = false); 612 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs; 613 614 bool os::Aix::is_sig_ignored(int sig) { 615 struct sigaction oact; 616 sigaction(sig, (struct sigaction*)NULL, &oact); 617 void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction) 618 : CAST_FROM_FN_PTR(void*, oact.sa_handler); 619 if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) { 620 return true; 621 } else { 622 return false; 623 } 624 } 625 626 void os::Aix::signal_sets_init() { 627 // Should also have an assertion stating we are still single-threaded. 628 assert(!signal_sets_initialized, "Already initialized"); 629 // Fill in signals that are necessarily unblocked for all threads in 630 // the VM. Currently, we unblock the following signals: 631 // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden 632 // by -Xrs (=ReduceSignalUsage)); 633 // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all 634 // other threads. The "ReduceSignalUsage" boolean tells us not to alter 635 // the dispositions or masks wrt these signals. 636 // Programs embedding the VM that want to use the above signals for their 637 // own purposes must, at this time, use the "-Xrs" option to prevent 638 // interference with shutdown hooks and BREAK_SIGNAL thread dumping. 639 // (See bug 4345157, and other related bugs). 640 // In reality, though, unblocking these signals is really a nop, since 641 // these signals are not blocked by default. 642 sigemptyset(&unblocked_sigs); 643 sigemptyset(&allowdebug_blocked_sigs); 644 sigaddset(&unblocked_sigs, SIGILL); 645 sigaddset(&unblocked_sigs, SIGSEGV); 646 sigaddset(&unblocked_sigs, SIGBUS); 647 sigaddset(&unblocked_sigs, SIGFPE); 648 sigaddset(&unblocked_sigs, SIGTRAP); 649 sigaddset(&unblocked_sigs, SIGDANGER); 650 sigaddset(&unblocked_sigs, SR_signum); 651 652 if (!ReduceSignalUsage) { 653 if (!os::Aix::is_sig_ignored(SHUTDOWN1_SIGNAL)) { 654 sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL); 655 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL); 656 } 657 if (!os::Aix::is_sig_ignored(SHUTDOWN2_SIGNAL)) { 658 sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL); 659 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL); 660 } 661 if (!os::Aix::is_sig_ignored(SHUTDOWN3_SIGNAL)) { 662 sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL); 663 sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL); 664 } 665 } 666 // Fill in signals that are blocked by all but the VM thread. 667 sigemptyset(&vm_sigs); 668 if (!ReduceSignalUsage) 669 sigaddset(&vm_sigs, BREAK_SIGNAL); 670 debug_only(signal_sets_initialized = true); 671 } 672 673 // These are signals that are unblocked while a thread is running Java. 674 // (For some reason, they get blocked by default.) 675 sigset_t* os::Aix::unblocked_signals() { 676 assert(signal_sets_initialized, "Not initialized"); 677 return &unblocked_sigs; 678 } 679 680 // These are the signals that are blocked while a (non-VM) thread is 681 // running Java. Only the VM thread handles these signals. 682 sigset_t* os::Aix::vm_signals() { 683 assert(signal_sets_initialized, "Not initialized"); 684 return &vm_sigs; 685 } 686 687 // These are signals that are blocked during cond_wait to allow debugger in 688 sigset_t* os::Aix::allowdebug_blocked_signals() { 689 assert(signal_sets_initialized, "Not initialized"); 690 return &allowdebug_blocked_sigs; 691 } 692 693 void os::Aix::hotspot_sigmask(Thread* thread) { 694 695 //Save caller's signal mask before setting VM signal mask 696 sigset_t caller_sigmask; 697 pthread_sigmask(SIG_BLOCK, NULL, &caller_sigmask); 698 699 OSThread* osthread = thread->osthread(); 700 osthread->set_caller_sigmask(caller_sigmask); 701 702 pthread_sigmask(SIG_UNBLOCK, os::Aix::unblocked_signals(), NULL); 703 704 if (!ReduceSignalUsage) { 705 if (thread->is_VM_thread()) { 706 // Only the VM thread handles BREAK_SIGNAL ... 707 pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL); 708 } else { 709 // ... all other threads block BREAK_SIGNAL 710 pthread_sigmask(SIG_BLOCK, vm_signals(), NULL); 711 } 712 } 713 } 714 715 // retrieve memory information. 716 // Returns false if something went wrong; 717 // content of pmi undefined in this case. 718 bool os::Aix::get_meminfo(meminfo_t* pmi) { 719 720 assert(pmi, "get_meminfo: invalid parameter"); 721 722 memset(pmi, 0, sizeof(meminfo_t)); 723 724 if (os::Aix::on_pase()) { 725 726 Unimplemented(); 727 return false; 728 729 } else { 730 731 // On AIX, I use the (dynamically loaded) perfstat library to retrieve memory statistics 732 // See: 733 // http://publib.boulder.ibm.com/infocenter/systems/index.jsp 734 // ?topic=/com.ibm.aix.basetechref/doc/basetrf1/perfstat_memtot.htm 735 // http://publib.boulder.ibm.com/infocenter/systems/index.jsp 736 // ?topic=/com.ibm.aix.files/doc/aixfiles/libperfstat.h.htm 737 738 perfstat_memory_total_t psmt; 739 memset (&psmt, '\0', sizeof(psmt)); 740 const int rc = libperfstat::perfstat_memory_total(NULL, &psmt, sizeof(psmt), 1); 741 if (rc == -1) { 742 fprintf(stderr, "perfstat_memory_total() failed (errno=%d)\n", errno); 743 assert(0, "perfstat_memory_total() failed"); 744 return false; 745 } 746 747 assert(rc == 1, "perfstat_memory_total() - weird return code"); 748 749 // excerpt from 750 // http://publib.boulder.ibm.com/infocenter/systems/index.jsp 751 // ?topic=/com.ibm.aix.files/doc/aixfiles/libperfstat.h.htm 752 // The fields of perfstat_memory_total_t: 753 // u_longlong_t virt_total Total virtual memory (in 4 KB pages). 754 // u_longlong_t real_total Total real memory (in 4 KB pages). 755 // u_longlong_t real_free Free real memory (in 4 KB pages). 756 // u_longlong_t pgsp_total Total paging space (in 4 KB pages). 757 // u_longlong_t pgsp_free Free paging space (in 4 KB pages). 758 759 pmi->virt_total = psmt.virt_total * 4096; 760 pmi->real_total = psmt.real_total * 4096; 761 pmi->real_free = psmt.real_free * 4096; 762 pmi->pgsp_total = psmt.pgsp_total * 4096; 763 pmi->pgsp_free = psmt.pgsp_free * 4096; 764 765 return true; 766 767 } 768 } // end os::Aix::get_meminfo 769 770 // Retrieve global cpu information. 771 // Returns false if something went wrong; 772 // the content of pci is undefined in this case. 773 bool os::Aix::get_cpuinfo(cpuinfo_t* pci) { 774 assert(pci, "get_cpuinfo: invalid parameter"); 775 memset(pci, 0, sizeof(cpuinfo_t)); 776 777 perfstat_cpu_total_t psct; 778 memset (&psct, '\0', sizeof(psct)); 779 780 if (-1 == libperfstat::perfstat_cpu_total(NULL, &psct, sizeof(perfstat_cpu_total_t), 1)) { 781 fprintf(stderr, "perfstat_cpu_total() failed (errno=%d)\n", errno); 782 assert(0, "perfstat_cpu_total() failed"); 783 return false; 784 } 785 786 // global cpu information 787 strcpy (pci->description, psct.description); 788 pci->processorHZ = psct.processorHZ; 789 pci->ncpus = psct.ncpus; 790 os::Aix::_logical_cpus = psct.ncpus; 791 for (int i = 0; i < 3; i++) { 792 pci->loadavg[i] = (double) psct.loadavg[i] / (1 << SBITS); 793 } 794 795 // get the processor version from _system_configuration 796 switch (_system_configuration.version) { 797 case PV_8: 798 strcpy(pci->version, "Power PC 8"); 799 break; 800 case PV_7: 801 strcpy(pci->version, "Power PC 7"); 802 break; 803 case PV_6_1: 804 strcpy(pci->version, "Power PC 6 DD1.x"); 805 break; 806 case PV_6: 807 strcpy(pci->version, "Power PC 6"); 808 break; 809 case PV_5: 810 strcpy(pci->version, "Power PC 5"); 811 break; 812 case PV_5_2: 813 strcpy(pci->version, "Power PC 5_2"); 814 break; 815 case PV_5_3: 816 strcpy(pci->version, "Power PC 5_3"); 817 break; 818 case PV_5_Compat: 819 strcpy(pci->version, "PV_5_Compat"); 820 break; 821 case PV_6_Compat: 822 strcpy(pci->version, "PV_6_Compat"); 823 break; 824 case PV_7_Compat: 825 strcpy(pci->version, "PV_7_Compat"); 826 break; 827 case PV_8_Compat: 828 strcpy(pci->version, "PV_8_Compat"); 829 break; 830 default: 831 strcpy(pci->version, "unknown"); 832 } 833 834 return true; 835 836 } //end os::Aix::get_cpuinfo 837 838 ////////////////////////////////////////////////////////////////////////////// 839 // detecting pthread library 840 841 void os::Aix::libpthread_init() { 842 return; 843 } 844 845 ////////////////////////////////////////////////////////////////////////////// 846 // create new thread 847 848 // Thread start routine for all newly created threads 849 static void *java_start(Thread *thread) { 850 851 // find out my own stack dimensions 852 { 853 // actually, this should do exactly the same as thread->record_stack_base_and_size... 854 address base = 0; 855 size_t size = 0; 856 query_stack_dimensions(&base, &size); 857 thread->set_stack_base(base); 858 thread->set_stack_size(size); 859 } 860 861 // Do some sanity checks. 862 CHECK_CURRENT_STACK_PTR(thread->stack_base(), thread->stack_size()); 863 864 // Try to randomize the cache line index of hot stack frames. 865 // This helps when threads of the same stack traces evict each other's 866 // cache lines. The threads can be either from the same JVM instance, or 867 // from different JVM instances. The benefit is especially true for 868 // processors with hyperthreading technology. 869 870 static int counter = 0; 871 int pid = os::current_process_id(); 872 alloca(((pid ^ counter++) & 7) * 128); 873 874 ThreadLocalStorage::set_thread(thread); 875 876 OSThread* osthread = thread->osthread(); 877 878 // thread_id is kernel thread id (similar to Solaris LWP id) 879 osthread->set_thread_id(os::Aix::gettid()); 880 881 // initialize signal mask for this thread 882 os::Aix::hotspot_sigmask(thread); 883 884 // initialize floating point control register 885 os::Aix::init_thread_fpu_state(); 886 887 assert(osthread->get_state() == RUNNABLE, "invalid os thread state"); 888 889 // call one more level start routine 890 thread->run(); 891 892 return 0; 893 } 894 895 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) { 896 897 // We want the whole function to be synchronized. 898 ThreadCritical cs; 899 900 assert(thread->osthread() == NULL, "caller responsible"); 901 902 // Allocate the OSThread object 903 OSThread* osthread = new OSThread(NULL, NULL); 904 if (osthread == NULL) { 905 return false; 906 } 907 908 // set the correct thread state 909 osthread->set_thread_type(thr_type); 910 911 // Initial state is ALLOCATED but not INITIALIZED 912 osthread->set_state(ALLOCATED); 913 914 thread->set_osthread(osthread); 915 916 // init thread attributes 917 pthread_attr_t attr; 918 pthread_attr_init(&attr); 919 guarantee(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0, "???"); 920 921 // Make sure we run in 1:1 kernel-user-thread mode. 922 if (os::Aix::on_aix()) { 923 guarantee(pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0, "???"); 924 guarantee(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0, "???"); 925 } // end: aix 926 927 // Start in suspended state, and in os::thread_start, wake the thread up. 928 guarantee(pthread_attr_setsuspendstate_np(&attr, PTHREAD_CREATE_SUSPENDED_NP) == 0, "???"); 929 930 // calculate stack size if it's not specified by caller 931 if (os::Aix::supports_variable_stack_size()) { 932 if (stack_size == 0) { 933 stack_size = os::Aix::default_stack_size(thr_type); 934 935 switch (thr_type) { 936 case os::java_thread: 937 // Java threads use ThreadStackSize whose default value can be changed with the flag -Xss. 938 assert(JavaThread::stack_size_at_create() > 0, "this should be set"); 939 stack_size = JavaThread::stack_size_at_create(); 940 break; 941 case os::compiler_thread: 942 if (CompilerThreadStackSize > 0) { 943 stack_size = (size_t)(CompilerThreadStackSize * K); 944 break; 945 } // else fall through: 946 // use VMThreadStackSize if CompilerThreadStackSize is not defined 947 case os::vm_thread: 948 case os::pgc_thread: 949 case os::cgc_thread: 950 case os::watcher_thread: 951 if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K); 952 break; 953 } 954 } 955 956 stack_size = MAX2(stack_size, os::Aix::min_stack_allowed); 957 pthread_attr_setstacksize(&attr, stack_size); 958 } //else let thread_create() pick the default value (96 K on AIX) 959 960 pthread_t tid; 961 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread); 962 963 pthread_attr_destroy(&attr); 964 965 if (ret == 0) { 966 // PPC port traceOsMisc(("Created New Thread : pthread-id %u", tid)); 967 } else { 968 if (PrintMiscellaneous && (Verbose || WizardMode)) { 969 perror("pthread_create()"); 970 } 971 // Need to clean up stuff we've allocated so far 972 thread->set_osthread(NULL); 973 delete osthread; 974 return false; 975 } 976 977 // Store pthread info into the OSThread 978 osthread->set_pthread_id(tid); 979 980 return true; 981 } 982 983 ///////////////////////////////////////////////////////////////////////////// 984 // attach existing thread 985 986 // bootstrap the main thread 987 bool os::create_main_thread(JavaThread* thread) { 988 assert(os::Aix::_main_thread == pthread_self(), "should be called inside main thread"); 989 return create_attached_thread(thread); 990 } 991 992 bool os::create_attached_thread(JavaThread* thread) { 993 #ifdef ASSERT 994 thread->verify_not_published(); 995 #endif 996 997 // Allocate the OSThread object 998 OSThread* osthread = new OSThread(NULL, NULL); 999 1000 if (osthread == NULL) { 1001 return false; 1002 } 1003 1004 // Store pthread info into the OSThread 1005 osthread->set_thread_id(os::Aix::gettid()); 1006 osthread->set_pthread_id(::pthread_self()); 1007 1008 // initialize floating point control register 1009 os::Aix::init_thread_fpu_state(); 1010 1011 // some sanity checks 1012 CHECK_CURRENT_STACK_PTR(thread->stack_base(), thread->stack_size()); 1013 1014 // Initial thread state is RUNNABLE 1015 osthread->set_state(RUNNABLE); 1016 1017 thread->set_osthread(osthread); 1018 1019 if (UseNUMA) { 1020 int lgrp_id = os::numa_get_group_id(); 1021 if (lgrp_id != -1) { 1022 thread->set_lgrp_id(lgrp_id); 1023 } 1024 } 1025 1026 // initialize signal mask for this thread 1027 // and save the caller's signal mask 1028 os::Aix::hotspot_sigmask(thread); 1029 1030 return true; 1031 } 1032 1033 void os::pd_start_thread(Thread* thread) { 1034 int status = pthread_continue_np(thread->osthread()->pthread_id()); 1035 assert(status == 0, "thr_continue failed"); 1036 } 1037 1038 // Free OS resources related to the OSThread 1039 void os::free_thread(OSThread* osthread) { 1040 assert(osthread != NULL, "osthread not set"); 1041 1042 if (Thread::current()->osthread() == osthread) { 1043 // Restore caller's signal mask 1044 sigset_t sigmask = osthread->caller_sigmask(); 1045 pthread_sigmask(SIG_SETMASK, &sigmask, NULL); 1046 } 1047 1048 delete osthread; 1049 } 1050 1051 ////////////////////////////////////////////////////////////////////////////// 1052 // thread local storage 1053 1054 int os::allocate_thread_local_storage() { 1055 pthread_key_t key; 1056 int rslt = pthread_key_create(&key, NULL); 1057 assert(rslt == 0, "cannot allocate thread local storage"); 1058 return (int)key; 1059 } 1060 1061 // Note: This is currently not used by VM, as we don't destroy TLS key 1062 // on VM exit. 1063 void os::free_thread_local_storage(int index) { 1064 int rslt = pthread_key_delete((pthread_key_t)index); 1065 assert(rslt == 0, "invalid index"); 1066 } 1067 1068 void os::thread_local_storage_at_put(int index, void* value) { 1069 int rslt = pthread_setspecific((pthread_key_t)index, value); 1070 assert(rslt == 0, "pthread_setspecific failed"); 1071 } 1072 1073 extern "C" Thread* get_thread() { 1074 return ThreadLocalStorage::thread(); 1075 } 1076 1077 //////////////////////////////////////////////////////////////////////////////// 1078 // time support 1079 1080 // Time since start-up in seconds to a fine granularity. 1081 // Used by VMSelfDestructTimer and the MemProfiler. 1082 double os::elapsedTime() { 1083 return (double)(os::elapsed_counter()) * 0.000001; 1084 } 1085 1086 jlong os::elapsed_counter() { 1087 timeval time; 1088 int status = gettimeofday(&time, NULL); 1089 return jlong(time.tv_sec) * 1000 * 1000 + jlong(time.tv_usec) - initial_time_count; 1090 } 1091 1092 jlong os::elapsed_frequency() { 1093 return (1000 * 1000); 1094 } 1095 1096 bool os::supports_vtime() { return true; } 1097 bool os::enable_vtime() { return false; } 1098 bool os::vtime_enabled() { return false; } 1099 1100 double os::elapsedVTime() { 1101 struct rusage usage; 1102 int retval = getrusage(RUSAGE_THREAD, &usage); 1103 if (retval == 0) { 1104 return usage.ru_utime.tv_sec + usage.ru_stime.tv_sec + (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / (1000.0 * 1000); 1105 } else { 1106 // better than nothing, but not much 1107 return elapsedTime(); 1108 } 1109 } 1110 1111 jlong os::javaTimeMillis() { 1112 timeval time; 1113 int status = gettimeofday(&time, NULL); 1114 assert(status != -1, "aix error at gettimeofday()"); 1115 return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000); 1116 } 1117 1118 // We need to manually declare mread_real_time, 1119 // because IBM didn't provide a prototype in time.h. 1120 // (they probably only ever tested in C, not C++) 1121 extern "C" 1122 int mread_real_time(timebasestruct_t *t, size_t size_of_timebasestruct_t); 1123 1124 jlong os::javaTimeNanos() { 1125 if (os::Aix::on_pase()) { 1126 Unimplemented(); 1127 return 0; 1128 } else { 1129 // On AIX use the precision of processors real time clock 1130 // or time base registers. 1131 timebasestruct_t time; 1132 int rc; 1133 1134 // If the CPU has a time register, it will be used and 1135 // we have to convert to real time first. After convertion we have following data: 1136 // time.tb_high [seconds since 00:00:00 UTC on 1.1.1970] 1137 // time.tb_low [nanoseconds after the last full second above] 1138 // We better use mread_real_time here instead of read_real_time 1139 // to ensure that we will get a monotonic increasing time. 1140 if (mread_real_time(&time, TIMEBASE_SZ) != RTC_POWER) { 1141 rc = time_base_to_time(&time, TIMEBASE_SZ); 1142 assert(rc != -1, "aix error at time_base_to_time()"); 1143 } 1144 return jlong(time.tb_high) * (1000 * 1000 * 1000) + jlong(time.tb_low); 1145 } 1146 } 1147 1148 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) { 1149 info_ptr->max_value = ALL_64_BITS; 1150 // mread_real_time() is monotonic (see 'os::javaTimeNanos()') 1151 info_ptr->may_skip_backward = false; 1152 info_ptr->may_skip_forward = false; 1153 info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time 1154 } 1155 1156 // Return the real, user, and system times in seconds from an 1157 // arbitrary fixed point in the past. 1158 bool os::getTimesSecs(double* process_real_time, 1159 double* process_user_time, 1160 double* process_system_time) { 1161 struct tms ticks; 1162 clock_t real_ticks = times(&ticks); 1163 1164 if (real_ticks == (clock_t) (-1)) { 1165 return false; 1166 } else { 1167 double ticks_per_second = (double) clock_tics_per_sec; 1168 *process_user_time = ((double) ticks.tms_utime) / ticks_per_second; 1169 *process_system_time = ((double) ticks.tms_stime) / ticks_per_second; 1170 *process_real_time = ((double) real_ticks) / ticks_per_second; 1171 1172 return true; 1173 } 1174 } 1175 1176 char * os::local_time_string(char *buf, size_t buflen) { 1177 struct tm t; 1178 time_t long_time; 1179 time(&long_time); 1180 localtime_r(&long_time, &t); 1181 jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d", 1182 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, 1183 t.tm_hour, t.tm_min, t.tm_sec); 1184 return buf; 1185 } 1186 1187 struct tm* os::localtime_pd(const time_t* clock, struct tm* res) { 1188 return localtime_r(clock, res); 1189 } 1190 1191 //////////////////////////////////////////////////////////////////////////////// 1192 // runtime exit support 1193 1194 // Note: os::shutdown() might be called very early during initialization, or 1195 // called from signal handler. Before adding something to os::shutdown(), make 1196 // sure it is async-safe and can handle partially initialized VM. 1197 void os::shutdown() { 1198 1199 // allow PerfMemory to attempt cleanup of any persistent resources 1200 perfMemory_exit(); 1201 1202 // needs to remove object in file system 1203 AttachListener::abort(); 1204 1205 // flush buffered output, finish log files 1206 ostream_abort(); 1207 1208 // Check for abort hook 1209 abort_hook_t abort_hook = Arguments::abort_hook(); 1210 if (abort_hook != NULL) { 1211 abort_hook(); 1212 } 1213 } 1214 1215 // Note: os::abort() might be called very early during initialization, or 1216 // called from signal handler. Before adding something to os::abort(), make 1217 // sure it is async-safe and can handle partially initialized VM. 1218 void os::abort(bool dump_core) { 1219 os::shutdown(); 1220 if (dump_core) { 1221 #ifndef PRODUCT 1222 fdStream out(defaultStream::output_fd()); 1223 out.print_raw("Current thread is "); 1224 char buf[16]; 1225 jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id()); 1226 out.print_raw_cr(buf); 1227 out.print_raw_cr("Dumping core ..."); 1228 #endif 1229 ::abort(); // dump core 1230 } 1231 1232 ::exit(1); 1233 } 1234 1235 // Die immediately, no exit hook, no abort hook, no cleanup. 1236 void os::die() { 1237 ::abort(); 1238 } 1239 1240 // This method is a copy of JDK's sysGetLastErrorString 1241 // from src/solaris/hpi/src/system_md.c 1242 1243 size_t os::lasterror(char *buf, size_t len) { 1244 if (errno == 0) return 0; 1245 1246 const char *s = ::strerror(errno); 1247 size_t n = ::strlen(s); 1248 if (n >= len) { 1249 n = len - 1; 1250 } 1251 ::strncpy(buf, s, n); 1252 buf[n] = '\0'; 1253 return n; 1254 } 1255 1256 intx os::current_thread_id() { return (intx)pthread_self(); } 1257 1258 int os::current_process_id() { 1259 1260 // This implementation returns a unique pid, the pid of the 1261 // launcher thread that starts the vm 'process'. 1262 1263 // Under POSIX, getpid() returns the same pid as the 1264 // launcher thread rather than a unique pid per thread. 1265 // Use gettid() if you want the old pre NPTL behaviour. 1266 1267 // if you are looking for the result of a call to getpid() that 1268 // returns a unique pid for the calling thread, then look at the 1269 // OSThread::thread_id() method in osThread_linux.hpp file 1270 1271 return (int)(_initial_pid ? _initial_pid : getpid()); 1272 } 1273 1274 // DLL functions 1275 1276 const char* os::dll_file_extension() { return ".so"; } 1277 1278 // This must be hard coded because it's the system's temporary 1279 // directory not the java application's temp directory, ala java.io.tmpdir. 1280 const char* os::get_temp_directory() { return "/tmp"; } 1281 1282 static bool file_exists(const char* filename) { 1283 struct stat statbuf; 1284 if (filename == NULL || strlen(filename) == 0) { 1285 return false; 1286 } 1287 return os::stat(filename, &statbuf) == 0; 1288 } 1289 1290 bool os::dll_build_name(char* buffer, size_t buflen, 1291 const char* pname, const char* fname) { 1292 bool retval = false; 1293 // Copied from libhpi 1294 const size_t pnamelen = pname ? strlen(pname) : 0; 1295 1296 // Return error on buffer overflow. 1297 if (pnamelen + strlen(fname) + 10 > (size_t) buflen) { 1298 *buffer = '\0'; 1299 return retval; 1300 } 1301 1302 if (pnamelen == 0) { 1303 snprintf(buffer, buflen, "lib%s.so", fname); 1304 retval = true; 1305 } else if (strchr(pname, *os::path_separator()) != NULL) { 1306 int n; 1307 char** pelements = split_path(pname, &n); 1308 for (int i = 0; i < n; i++) { 1309 // Really shouldn't be NULL, but check can't hurt 1310 if (pelements[i] == NULL || strlen(pelements[i]) == 0) { 1311 continue; // skip the empty path values 1312 } 1313 snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname); 1314 if (file_exists(buffer)) { 1315 retval = true; 1316 break; 1317 } 1318 } 1319 // release the storage 1320 for (int i = 0; i < n; i++) { 1321 if (pelements[i] != NULL) { 1322 FREE_C_HEAP_ARRAY(char, pelements[i]); 1323 } 1324 } 1325 if (pelements != NULL) { 1326 FREE_C_HEAP_ARRAY(char*, pelements); 1327 } 1328 } else { 1329 snprintf(buffer, buflen, "%s/lib%s.so", pname, fname); 1330 retval = true; 1331 } 1332 return retval; 1333 } 1334 1335 // Check if addr is inside libjvm.so. 1336 bool os::address_is_in_vm(address addr) { 1337 1338 // Input could be a real pc or a function pointer literal. The latter 1339 // would be a function descriptor residing in the data segment of a module. 1340 1341 const LoadedLibraryModule* lib = LoadedLibraries::find_for_text_address(addr); 1342 if (lib) { 1343 if (strcmp(lib->get_shortname(), "libjvm.so") == 0) { 1344 return true; 1345 } else { 1346 return false; 1347 } 1348 } else { 1349 lib = LoadedLibraries::find_for_data_address(addr); 1350 if (lib) { 1351 if (strcmp(lib->get_shortname(), "libjvm.so") == 0) { 1352 return true; 1353 } else { 1354 return false; 1355 } 1356 } else { 1357 return false; 1358 } 1359 } 1360 } 1361 1362 // Resolve an AIX function descriptor literal to a code pointer. 1363 // If the input is a valid code pointer to a text segment of a loaded module, 1364 // it is returned unchanged. 1365 // If the input is a valid AIX function descriptor, it is resolved to the 1366 // code entry point. 1367 // If the input is neither a valid function descriptor nor a valid code pointer, 1368 // NULL is returned. 1369 static address resolve_function_descriptor_to_code_pointer(address p) { 1370 1371 const LoadedLibraryModule* lib = LoadedLibraries::find_for_text_address(p); 1372 if (lib) { 1373 // its a real code pointer 1374 return p; 1375 } else { 1376 lib = LoadedLibraries::find_for_data_address(p); 1377 if (lib) { 1378 // pointer to data segment, potential function descriptor 1379 address code_entry = (address)(((FunctionDescriptor*)p)->entry()); 1380 if (LoadedLibraries::find_for_text_address(code_entry)) { 1381 // Its a function descriptor 1382 return code_entry; 1383 } 1384 } 1385 } 1386 return NULL; 1387 } 1388 1389 bool os::dll_address_to_function_name(address addr, char *buf, 1390 int buflen, int *offset) { 1391 if (offset) { 1392 *offset = -1; 1393 } 1394 // Buf is not optional, but offset is optional. 1395 assert(buf != NULL, "sanity check"); 1396 buf[0] = '\0'; 1397 1398 // Resolve function ptr literals first. 1399 addr = resolve_function_descriptor_to_code_pointer(addr); 1400 if (!addr) { 1401 return false; 1402 } 1403 1404 // Go through Decoder::decode to call getFuncName which reads the name from the traceback table. 1405 return Decoder::decode(addr, buf, buflen, offset); 1406 } 1407 1408 static int getModuleName(codeptr_t pc, // [in] program counter 1409 char* p_name, size_t namelen, // [out] optional: function name 1410 char* p_errmsg, size_t errmsglen // [out] optional: user provided buffer for error messages 1411 ) { 1412 1413 // initialize output parameters 1414 if (p_name && namelen > 0) { 1415 *p_name = '\0'; 1416 } 1417 if (p_errmsg && errmsglen > 0) { 1418 *p_errmsg = '\0'; 1419 } 1420 1421 const LoadedLibraryModule* const lib = LoadedLibraries::find_for_text_address((address)pc); 1422 if (lib) { 1423 if (p_name && namelen > 0) { 1424 sprintf(p_name, "%.*s", namelen, lib->get_shortname()); 1425 } 1426 return 0; 1427 } 1428 1429 trcVerbose("pc outside any module"); 1430 1431 return -1; 1432 } 1433 1434 bool os::dll_address_to_library_name(address addr, char* buf, 1435 int buflen, int* offset) { 1436 if (offset) { 1437 *offset = -1; 1438 } 1439 // Buf is not optional, but offset is optional. 1440 assert(buf != NULL, "sanity check"); 1441 buf[0] = '\0'; 1442 1443 // Resolve function ptr literals first. 1444 addr = resolve_function_descriptor_to_code_pointer(addr); 1445 if (!addr) { 1446 return false; 1447 } 1448 1449 if (::getModuleName((codeptr_t) addr, buf, buflen, 0, 0) == 0) { 1450 return true; 1451 } 1452 return false; 1453 } 1454 1455 // Loads .dll/.so and in case of error it checks if .dll/.so was built 1456 // for the same architecture as Hotspot is running on. 1457 void *os::dll_load(const char *filename, char *ebuf, int ebuflen) { 1458 1459 if (ebuf && ebuflen > 0) { 1460 ebuf[0] = '\0'; 1461 ebuf[ebuflen - 1] = '\0'; 1462 } 1463 1464 if (!filename || strlen(filename) == 0) { 1465 ::strncpy(ebuf, "dll_load: empty filename specified", ebuflen - 1); 1466 return NULL; 1467 } 1468 1469 // RTLD_LAZY is currently not implemented. The dl is loaded immediately with all its dependants. 1470 void * result= ::dlopen(filename, RTLD_LAZY); 1471 if (result != NULL) { 1472 // Reload dll cache. Don't do this in signal handling. 1473 LoadedLibraries::reload(); 1474 return result; 1475 } else { 1476 // error analysis when dlopen fails 1477 const char* const error_report = ::dlerror(); 1478 if (error_report && ebuf && ebuflen > 0) { 1479 snprintf(ebuf, ebuflen - 1, "%s, LIBPATH=%s, LD_LIBRARY_PATH=%s : %s", 1480 filename, ::getenv("LIBPATH"), ::getenv("LD_LIBRARY_PATH"), error_report); 1481 } 1482 } 1483 return NULL; 1484 } 1485 1486 // Glibc-2.0 libdl is not MT safe. If you are building with any glibc, 1487 // chances are you might want to run the generated bits against glibc-2.0 1488 // libdl.so, so always use locking for any version of glibc. 1489 void* os::dll_lookup(void* handle, const char* name) { 1490 pthread_mutex_lock(&dl_mutex); 1491 void* res = dlsym(handle, name); 1492 pthread_mutex_unlock(&dl_mutex); 1493 return res; 1494 } 1495 1496 void* os::get_default_process_handle() { 1497 return (void*)::dlopen(NULL, RTLD_LAZY); 1498 } 1499 1500 void os::print_dll_info(outputStream *st) { 1501 st->print_cr("Dynamic libraries:"); 1502 LoadedLibraries::print(st); 1503 } 1504 1505 void os::print_os_info(outputStream* st) { 1506 st->print("OS:"); 1507 1508 st->print("uname:"); 1509 struct utsname name; 1510 uname(&name); 1511 st->print(name.sysname); st->print(" "); 1512 st->print(name.nodename); st->print(" "); 1513 st->print(name.release); st->print(" "); 1514 st->print(name.version); st->print(" "); 1515 st->print(name.machine); 1516 st->cr(); 1517 1518 // rlimit 1519 st->print("rlimit:"); 1520 struct rlimit rlim; 1521 1522 st->print(" STACK "); 1523 getrlimit(RLIMIT_STACK, &rlim); 1524 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 1525 else st->print("%uk", rlim.rlim_cur >> 10); 1526 1527 st->print(", CORE "); 1528 getrlimit(RLIMIT_CORE, &rlim); 1529 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 1530 else st->print("%uk", rlim.rlim_cur >> 10); 1531 1532 st->print(", NPROC "); 1533 st->print("%d", sysconf(_SC_CHILD_MAX)); 1534 1535 st->print(", NOFILE "); 1536 getrlimit(RLIMIT_NOFILE, &rlim); 1537 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 1538 else st->print("%d", rlim.rlim_cur); 1539 1540 st->print(", AS "); 1541 getrlimit(RLIMIT_AS, &rlim); 1542 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 1543 else st->print("%uk", rlim.rlim_cur >> 10); 1544 1545 // Print limits on DATA, because it limits the C-heap. 1546 st->print(", DATA "); 1547 getrlimit(RLIMIT_DATA, &rlim); 1548 if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity"); 1549 else st->print("%uk", rlim.rlim_cur >> 10); 1550 st->cr(); 1551 1552 // load average 1553 st->print("load average:"); 1554 double loadavg[3] = {-1.L, -1.L, -1.L}; 1555 os::loadavg(loadavg, 3); 1556 st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]); 1557 st->cr(); 1558 } 1559 1560 void os::print_memory_info(outputStream* st) { 1561 1562 st->print_cr("Memory:"); 1563 1564 st->print_cr(" default page size: %s", describe_pagesize(os::vm_page_size())); 1565 st->print_cr(" default stack page size: %s", describe_pagesize(os::vm_page_size())); 1566 st->print_cr(" default shm page size: %s", describe_pagesize(os::Aix::shm_default_page_size())); 1567 st->print_cr(" can use 64K pages dynamically: %s", (os::Aix::can_use_64K_pages() ? "yes" :"no")); 1568 st->print_cr(" can use 16M pages dynamically: %s", (os::Aix::can_use_16M_pages() ? "yes" :"no")); 1569 if (g_multipage_error != 0) { 1570 st->print_cr(" multipage error: %d", g_multipage_error); 1571 } 1572 1573 // print out LDR_CNTRL because it affects the default page sizes 1574 const char* const ldr_cntrl = ::getenv("LDR_CNTRL"); 1575 st->print_cr(" LDR_CNTRL=%s.", ldr_cntrl ? ldr_cntrl : "<unset>"); 1576 1577 const char* const extshm = ::getenv("EXTSHM"); 1578 st->print_cr(" EXTSHM=%s.", extshm ? extshm : "<unset>"); 1579 1580 // Call os::Aix::get_meminfo() to retrieve memory statistics. 1581 os::Aix::meminfo_t mi; 1582 if (os::Aix::get_meminfo(&mi)) { 1583 char buffer[256]; 1584 if (os::Aix::on_aix()) { 1585 jio_snprintf(buffer, sizeof(buffer), 1586 " physical total : %llu\n" 1587 " physical free : %llu\n" 1588 " swap total : %llu\n" 1589 " swap free : %llu\n", 1590 mi.real_total, 1591 mi.real_free, 1592 mi.pgsp_total, 1593 mi.pgsp_free); 1594 } else { 1595 Unimplemented(); 1596 } 1597 st->print_raw(buffer); 1598 } else { 1599 st->print_cr(" (no more information available)"); 1600 } 1601 } 1602 1603 void os::pd_print_cpu_info(outputStream* st) { 1604 // cpu 1605 st->print("CPU:"); 1606 st->print("total %d", os::processor_count()); 1607 // It's not safe to query number of active processors after crash 1608 // st->print("(active %d)", os::active_processor_count()); 1609 st->print(" %s", VM_Version::cpu_features()); 1610 st->cr(); 1611 } 1612 1613 void os::print_siginfo(outputStream* st, void* siginfo) { 1614 // Use common posix version. 1615 os::Posix::print_siginfo_brief(st, (const siginfo_t*) siginfo); 1616 st->cr(); 1617 } 1618 1619 static void print_signal_handler(outputStream* st, int sig, 1620 char* buf, size_t buflen); 1621 1622 void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) { 1623 st->print_cr("Signal Handlers:"); 1624 print_signal_handler(st, SIGSEGV, buf, buflen); 1625 print_signal_handler(st, SIGBUS , buf, buflen); 1626 print_signal_handler(st, SIGFPE , buf, buflen); 1627 print_signal_handler(st, SIGPIPE, buf, buflen); 1628 print_signal_handler(st, SIGXFSZ, buf, buflen); 1629 print_signal_handler(st, SIGILL , buf, buflen); 1630 print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen); 1631 print_signal_handler(st, SR_signum, buf, buflen); 1632 print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen); 1633 print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen); 1634 print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen); 1635 print_signal_handler(st, BREAK_SIGNAL, buf, buflen); 1636 print_signal_handler(st, SIGTRAP, buf, buflen); 1637 print_signal_handler(st, SIGDANGER, buf, buflen); 1638 } 1639 1640 static char saved_jvm_path[MAXPATHLEN] = {0}; 1641 1642 // Find the full path to the current module, libjvm.so. 1643 void os::jvm_path(char *buf, jint buflen) { 1644 // Error checking. 1645 if (buflen < MAXPATHLEN) { 1646 assert(false, "must use a large-enough buffer"); 1647 buf[0] = '\0'; 1648 return; 1649 } 1650 // Lazy resolve the path to current module. 1651 if (saved_jvm_path[0] != 0) { 1652 strcpy(buf, saved_jvm_path); 1653 return; 1654 } 1655 1656 Dl_info dlinfo; 1657 int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo); 1658 assert(ret != 0, "cannot locate libjvm"); 1659 char* rp = realpath((char *)dlinfo.dli_fname, buf); 1660 assert(rp != NULL, "error in realpath(): maybe the 'path' argument is too long?"); 1661 1662 strncpy(saved_jvm_path, buf, sizeof(saved_jvm_path)); 1663 saved_jvm_path[sizeof(saved_jvm_path) - 1] = '\0'; 1664 } 1665 1666 void os::print_jni_name_prefix_on(outputStream* st, int args_size) { 1667 // no prefix required, not even "_" 1668 } 1669 1670 void os::print_jni_name_suffix_on(outputStream* st, int args_size) { 1671 // no suffix required 1672 } 1673 1674 //////////////////////////////////////////////////////////////////////////////// 1675 // sun.misc.Signal support 1676 1677 static volatile jint sigint_count = 0; 1678 1679 static void 1680 UserHandler(int sig, void *siginfo, void *context) { 1681 // 4511530 - sem_post is serialized and handled by the manager thread. When 1682 // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We 1683 // don't want to flood the manager thread with sem_post requests. 1684 if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1) 1685 return; 1686 1687 // Ctrl-C is pressed during error reporting, likely because the error 1688 // handler fails to abort. Let VM die immediately. 1689 if (sig == SIGINT && is_error_reported()) { 1690 os::die(); 1691 } 1692 1693 os::signal_notify(sig); 1694 } 1695 1696 void* os::user_handler() { 1697 return CAST_FROM_FN_PTR(void*, UserHandler); 1698 } 1699 1700 extern "C" { 1701 typedef void (*sa_handler_t)(int); 1702 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); 1703 } 1704 1705 void* os::signal(int signal_number, void* handler) { 1706 struct sigaction sigAct, oldSigAct; 1707 1708 sigfillset(&(sigAct.sa_mask)); 1709 1710 // Do not block out synchronous signals in the signal handler. 1711 // Blocking synchronous signals only makes sense if you can really 1712 // be sure that those signals won't happen during signal handling, 1713 // when the blocking applies. Normal signal handlers are lean and 1714 // do not cause signals. But our signal handlers tend to be "risky" 1715 // - secondary SIGSEGV, SIGILL, SIGBUS' may and do happen. 1716 // On AIX, PASE there was a case where a SIGSEGV happened, followed 1717 // by a SIGILL, which was blocked due to the signal mask. The process 1718 // just hung forever. Better to crash from a secondary signal than to hang. 1719 sigdelset(&(sigAct.sa_mask), SIGSEGV); 1720 sigdelset(&(sigAct.sa_mask), SIGBUS); 1721 sigdelset(&(sigAct.sa_mask), SIGILL); 1722 sigdelset(&(sigAct.sa_mask), SIGFPE); 1723 sigdelset(&(sigAct.sa_mask), SIGTRAP); 1724 1725 sigAct.sa_flags = SA_RESTART|SA_SIGINFO; 1726 1727 sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler); 1728 1729 if (sigaction(signal_number, &sigAct, &oldSigAct)) { 1730 // -1 means registration failed 1731 return (void *)-1; 1732 } 1733 1734 return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler); 1735 } 1736 1737 void os::signal_raise(int signal_number) { 1738 ::raise(signal_number); 1739 } 1740 1741 // 1742 // The following code is moved from os.cpp for making this 1743 // code platform specific, which it is by its very nature. 1744 // 1745 1746 // Will be modified when max signal is changed to be dynamic 1747 int os::sigexitnum_pd() { 1748 return NSIG; 1749 } 1750 1751 // a counter for each possible signal value 1752 static volatile jint pending_signals[NSIG+1] = { 0 }; 1753 1754 // Linux(POSIX) specific hand shaking semaphore. 1755 static sem_t sig_sem; 1756 1757 void os::signal_init_pd() { 1758 // Initialize signal structures 1759 ::memset((void*)pending_signals, 0, sizeof(pending_signals)); 1760 1761 // Initialize signal semaphore 1762 int rc = ::sem_init(&sig_sem, 0, 0); 1763 guarantee(rc != -1, "sem_init failed"); 1764 } 1765 1766 void os::signal_notify(int sig) { 1767 Atomic::inc(&pending_signals[sig]); 1768 ::sem_post(&sig_sem); 1769 } 1770 1771 static int check_pending_signals(bool wait) { 1772 Atomic::store(0, &sigint_count); 1773 for (;;) { 1774 for (int i = 0; i < NSIG + 1; i++) { 1775 jint n = pending_signals[i]; 1776 if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) { 1777 return i; 1778 } 1779 } 1780 if (!wait) { 1781 return -1; 1782 } 1783 JavaThread *thread = JavaThread::current(); 1784 ThreadBlockInVM tbivm(thread); 1785 1786 bool threadIsSuspended; 1787 do { 1788 thread->set_suspend_equivalent(); 1789 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 1790 1791 ::sem_wait(&sig_sem); 1792 1793 // were we externally suspended while we were waiting? 1794 threadIsSuspended = thread->handle_special_suspend_equivalent_condition(); 1795 if (threadIsSuspended) { 1796 // 1797 // The semaphore has been incremented, but while we were waiting 1798 // another thread suspended us. We don't want to continue running 1799 // while suspended because that would surprise the thread that 1800 // suspended us. 1801 // 1802 ::sem_post(&sig_sem); 1803 1804 thread->java_suspend_self(); 1805 } 1806 } while (threadIsSuspended); 1807 } 1808 } 1809 1810 int os::signal_lookup() { 1811 return check_pending_signals(false); 1812 } 1813 1814 int os::signal_wait() { 1815 return check_pending_signals(true); 1816 } 1817 1818 //////////////////////////////////////////////////////////////////////////////// 1819 // Virtual Memory 1820 1821 // AddrRange describes an immutable address range 1822 // 1823 // This is a helper class for the 'shared memory bookkeeping' below. 1824 class AddrRange { 1825 friend class ShmBkBlock; 1826 1827 char* _start; 1828 size_t _size; 1829 1830 public: 1831 1832 AddrRange(char* start, size_t size) 1833 : _start(start), _size(size) 1834 {} 1835 1836 AddrRange(const AddrRange& r) 1837 : _start(r.start()), _size(r.size()) 1838 {} 1839 1840 char* start() const { return _start; } 1841 size_t size() const { return _size; } 1842 char* end() const { return _start + _size; } 1843 bool is_empty() const { return _size == 0 ? true : false; } 1844 1845 static AddrRange empty_range() { return AddrRange(NULL, 0); } 1846 1847 bool contains(const char* p) const { 1848 return start() <= p && end() > p; 1849 } 1850 1851 bool contains(const AddrRange& range) const { 1852 return start() <= range.start() && end() >= range.end(); 1853 } 1854 1855 bool intersects(const AddrRange& range) const { 1856 return (range.start() <= start() && range.end() > start()) || 1857 (range.start() < end() && range.end() >= end()) || 1858 contains(range); 1859 } 1860 1861 bool is_same_range(const AddrRange& range) const { 1862 return start() == range.start() && size() == range.size(); 1863 } 1864 1865 // return the closest inside range consisting of whole pages 1866 AddrRange find_closest_aligned_range(size_t pagesize) const { 1867 if (pagesize == 0 || is_empty()) { 1868 return empty_range(); 1869 } 1870 char* const from = (char*)align_size_up((intptr_t)_start, pagesize); 1871 char* const to = (char*)align_size_down((intptr_t)end(), pagesize); 1872 if (from > to) { 1873 return empty_range(); 1874 } 1875 return AddrRange(from, to - from); 1876 } 1877 }; 1878 1879 //////////////////////////////////////////////////////////////////////////// 1880 // shared memory bookkeeping 1881 // 1882 // the os::reserve_memory() API and friends hand out different kind of memory, depending 1883 // on need and circumstances. Memory may be allocated with mmap() or with shmget/shmat. 1884 // 1885 // But these memory types have to be treated differently. For example, to uncommit 1886 // mmap-based memory, msync(MS_INVALIDATE) is needed, to uncommit shmat-based memory, 1887 // disclaim64() is needed. 1888 // 1889 // Therefore we need to keep track of the allocated memory segments and their 1890 // properties. 1891 1892 // ShmBkBlock: base class for all blocks in the shared memory bookkeeping 1893 class ShmBkBlock : public CHeapObj<mtInternal> { 1894 1895 ShmBkBlock* _next; 1896 1897 protected: 1898 1899 AddrRange _range; 1900 const size_t _pagesize; 1901 const bool _pinned; 1902 1903 public: 1904 1905 ShmBkBlock(AddrRange range, size_t pagesize, bool pinned) 1906 : _range(range), _pagesize(pagesize), _pinned(pinned) , _next(NULL) { 1907 1908 assert(_pagesize == SIZE_4K || _pagesize == SIZE_64K || _pagesize == SIZE_16M, "invalid page size"); 1909 assert(!_range.is_empty(), "invalid range"); 1910 } 1911 1912 virtual void print(outputStream* st) const { 1913 st->print("0x%p ... 0x%p (%llu) - %d %s pages - %s", 1914 _range.start(), _range.end(), _range.size(), 1915 _range.size() / _pagesize, describe_pagesize(_pagesize), 1916 _pinned ? "pinned" : ""); 1917 } 1918 1919 enum Type { MMAP, SHMAT }; 1920 virtual Type getType() = 0; 1921 1922 char* base() const { return _range.start(); } 1923 size_t size() const { return _range.size(); } 1924 1925 void setAddrRange(AddrRange range) { 1926 _range = range; 1927 } 1928 1929 bool containsAddress(const char* p) const { 1930 return _range.contains(p); 1931 } 1932 1933 bool containsRange(const char* p, size_t size) const { 1934 return _range.contains(AddrRange((char*)p, size)); 1935 } 1936 1937 bool isSameRange(const char* p, size_t size) const { 1938 return _range.is_same_range(AddrRange((char*)p, size)); 1939 } 1940 1941 virtual bool disclaim(char* p, size_t size) = 0; 1942 virtual bool release() = 0; 1943 1944 // blocks live in a list. 1945 ShmBkBlock* next() const { return _next; } 1946 void set_next(ShmBkBlock* blk) { _next = blk; } 1947 1948 }; // end: ShmBkBlock 1949 1950 1951 // ShmBkMappedBlock: describes an block allocated with mmap() 1952 class ShmBkMappedBlock : public ShmBkBlock { 1953 public: 1954 1955 ShmBkMappedBlock(AddrRange range) 1956 : ShmBkBlock(range, SIZE_4K, false) {} // mmap: always 4K, never pinned 1957 1958 void print(outputStream* st) const { 1959 ShmBkBlock::print(st); 1960 st->print_cr(" - mmap'ed"); 1961 } 1962 1963 Type getType() { 1964 return MMAP; 1965 } 1966 1967 bool disclaim(char* p, size_t size) { 1968 1969 AddrRange r(p, size); 1970 1971 guarantee(_range.contains(r), "invalid disclaim"); 1972 1973 // only disclaim whole ranges. 1974 const AddrRange r2 = r.find_closest_aligned_range(_pagesize); 1975 if (r2.is_empty()) { 1976 return true; 1977 } 1978 1979 const int rc = ::msync(r2.start(), r2.size(), MS_INVALIDATE); 1980 1981 if (rc != 0) { 1982 warning("msync(0x%p, %llu, MS_INVALIDATE) failed (%d)\n", r2.start(), r2.size(), errno); 1983 } 1984 1985 return rc == 0 ? true : false; 1986 } 1987 1988 bool release() { 1989 // mmap'ed blocks are released using munmap 1990 if (::munmap(_range.start(), _range.size()) != 0) { 1991 warning("munmap(0x%p, %llu) failed (%d)\n", _range.start(), _range.size(), errno); 1992 return false; 1993 } 1994 return true; 1995 } 1996 }; // end: ShmBkMappedBlock 1997 1998 // ShmBkShmatedBlock: describes an block allocated with shmget/shmat() 1999 class ShmBkShmatedBlock : public ShmBkBlock { 2000 public: 2001 2002 ShmBkShmatedBlock(AddrRange range, size_t pagesize, bool pinned) 2003 : ShmBkBlock(range, pagesize, pinned) {} 2004 2005 void print(outputStream* st) const { 2006 ShmBkBlock::print(st); 2007 st->print_cr(" - shmat'ed"); 2008 } 2009 2010 Type getType() { 2011 return SHMAT; 2012 } 2013 2014 bool disclaim(char* p, size_t size) { 2015 2016 AddrRange r(p, size); 2017 2018 if (_pinned) { 2019 return true; 2020 } 2021 2022 // shmat'ed blocks are disclaimed using disclaim64 2023 guarantee(_range.contains(r), "invalid disclaim"); 2024 2025 // only disclaim whole ranges. 2026 const AddrRange r2 = r.find_closest_aligned_range(_pagesize); 2027 if (r2.is_empty()) { 2028 return true; 2029 } 2030 2031 const bool rc = my_disclaim64(r2.start(), r2.size()); 2032 2033 if (Verbose && !rc) { 2034 warning("failed to disclaim shm %p-%p\n", r2.start(), r2.end()); 2035 } 2036 2037 return rc; 2038 } 2039 2040 bool release() { 2041 bool rc = false; 2042 if (::shmdt(_range.start()) != 0) { 2043 warning("shmdt(0x%p) failed (%d)\n", _range.start(), errno); 2044 } else { 2045 rc = true; 2046 } 2047 return rc; 2048 } 2049 2050 }; // end: ShmBkShmatedBlock 2051 2052 static ShmBkBlock* g_shmbk_list = NULL; 2053 static volatile jint g_shmbk_table_lock = 0; 2054 2055 // keep some usage statistics 2056 static struct { 2057 int nodes; // number of nodes in list 2058 size_t bytes; // reserved - not committed - bytes. 2059 int reserves; // how often reserve was called 2060 int lookups; // how often a lookup was made 2061 } g_shmbk_stats = { 0, 0, 0, 0 }; 2062 2063 // add information about a shared memory segment to the bookkeeping 2064 static void shmbk_register(ShmBkBlock* p_block) { 2065 guarantee(p_block, "logic error"); 2066 p_block->set_next(g_shmbk_list); 2067 g_shmbk_list = p_block; 2068 g_shmbk_stats.reserves ++; 2069 g_shmbk_stats.bytes += p_block->size(); 2070 g_shmbk_stats.nodes ++; 2071 } 2072 2073 // remove information about a shared memory segment by its starting address 2074 static void shmbk_unregister(ShmBkBlock* p_block) { 2075 ShmBkBlock* p = g_shmbk_list; 2076 ShmBkBlock* prev = NULL; 2077 while (p) { 2078 if (p == p_block) { 2079 if (prev) { 2080 prev->set_next(p->next()); 2081 } else { 2082 g_shmbk_list = p->next(); 2083 } 2084 g_shmbk_stats.nodes --; 2085 g_shmbk_stats.bytes -= p->size(); 2086 return; 2087 } 2088 prev = p; 2089 p = p->next(); 2090 } 2091 assert(false, "should not happen"); 2092 } 2093 2094 // given a pointer, return shared memory bookkeeping record for the segment it points into 2095 // using the returned block info must happen under lock protection 2096 static ShmBkBlock* shmbk_find_by_containing_address(const char* addr) { 2097 g_shmbk_stats.lookups ++; 2098 ShmBkBlock* p = g_shmbk_list; 2099 while (p) { 2100 if (p->containsAddress(addr)) { 2101 return p; 2102 } 2103 p = p->next(); 2104 } 2105 return NULL; 2106 } 2107 2108 // dump all information about all memory segments allocated with os::reserve_memory() 2109 void shmbk_dump_info() { 2110 tty->print_cr("-- shared mem bookkeeping (alive: %d segments, %llu bytes, " 2111 "total reserves: %d total lookups: %d)", 2112 g_shmbk_stats.nodes, g_shmbk_stats.bytes, g_shmbk_stats.reserves, g_shmbk_stats.lookups); 2113 const ShmBkBlock* p = g_shmbk_list; 2114 int i = 0; 2115 while (p) { 2116 p->print(tty); 2117 p = p->next(); 2118 i ++; 2119 } 2120 } 2121 2122 #define LOCK_SHMBK { ThreadCritical _LOCK_SHMBK; 2123 #define UNLOCK_SHMBK } 2124 2125 // End: shared memory bookkeeping 2126 //////////////////////////////////////////////////////////////////////////////////////////////////// 2127 2128 int os::vm_page_size() { 2129 // Seems redundant as all get out 2130 assert(os::Aix::page_size() != -1, "must call os::init"); 2131 return os::Aix::page_size(); 2132 } 2133 2134 // Aix allocates memory by pages. 2135 int os::vm_allocation_granularity() { 2136 assert(os::Aix::page_size() != -1, "must call os::init"); 2137 return os::Aix::page_size(); 2138 } 2139 2140 int os::Aix::commit_memory_impl(char* addr, size_t size, bool exec) { 2141 2142 // Commit is a noop. There is no explicit commit 2143 // needed on AIX. Memory is committed when touched. 2144 // 2145 // Debug : check address range for validity 2146 #ifdef ASSERT 2147 LOCK_SHMBK 2148 ShmBkBlock* const block = shmbk_find_by_containing_address(addr); 2149 if (!block) { 2150 fprintf(stderr, "invalid pointer: " INTPTR_FORMAT "\n", addr); 2151 shmbk_dump_info(); 2152 assert(false, "invalid pointer"); 2153 return false; 2154 } else if (!block->containsRange(addr, size)) { 2155 fprintf(stderr, "invalid range: " INTPTR_FORMAT " .. " INTPTR_FORMAT "\n", addr, addr + size); 2156 shmbk_dump_info(); 2157 assert(false, "invalid range"); 2158 return false; 2159 } 2160 UNLOCK_SHMBK 2161 #endif // ASSERT 2162 2163 return 0; 2164 } 2165 2166 bool os::pd_commit_memory(char* addr, size_t size, bool exec) { 2167 return os::Aix::commit_memory_impl(addr, size, exec) == 0; 2168 } 2169 2170 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec, 2171 const char* mesg) { 2172 assert(mesg != NULL, "mesg must be specified"); 2173 os::Aix::commit_memory_impl(addr, size, exec); 2174 } 2175 2176 int os::Aix::commit_memory_impl(char* addr, size_t size, 2177 size_t alignment_hint, bool exec) { 2178 return os::Aix::commit_memory_impl(addr, size, exec); 2179 } 2180 2181 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint, 2182 bool exec) { 2183 return os::Aix::commit_memory_impl(addr, size, alignment_hint, exec) == 0; 2184 } 2185 2186 void os::pd_commit_memory_or_exit(char* addr, size_t size, 2187 size_t alignment_hint, bool exec, 2188 const char* mesg) { 2189 os::Aix::commit_memory_impl(addr, size, alignment_hint, exec); 2190 } 2191 2192 bool os::pd_uncommit_memory(char* addr, size_t size) { 2193 2194 // Delegate to ShmBkBlock class which knows how to uncommit its memory. 2195 2196 bool rc = false; 2197 LOCK_SHMBK 2198 ShmBkBlock* const block = shmbk_find_by_containing_address(addr); 2199 if (!block) { 2200 fprintf(stderr, "invalid pointer: 0x%p.\n", addr); 2201 shmbk_dump_info(); 2202 assert(false, "invalid pointer"); 2203 return false; 2204 } else if (!block->containsRange(addr, size)) { 2205 fprintf(stderr, "invalid range: 0x%p .. 0x%p.\n", addr, addr + size); 2206 shmbk_dump_info(); 2207 assert(false, "invalid range"); 2208 return false; 2209 } 2210 rc = block->disclaim(addr, size); 2211 UNLOCK_SHMBK 2212 2213 if (Verbose && !rc) { 2214 warning("failed to disclaim 0x%p .. 0x%p (0x%llX bytes).", addr, addr + size, size); 2215 } 2216 return rc; 2217 } 2218 2219 bool os::pd_create_stack_guard_pages(char* addr, size_t size) { 2220 return os::guard_memory(addr, size); 2221 } 2222 2223 bool os::remove_stack_guard_pages(char* addr, size_t size) { 2224 return os::unguard_memory(addr, size); 2225 } 2226 2227 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) { 2228 } 2229 2230 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) { 2231 } 2232 2233 void os::numa_make_global(char *addr, size_t bytes) { 2234 } 2235 2236 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) { 2237 } 2238 2239 bool os::numa_topology_changed() { 2240 return false; 2241 } 2242 2243 size_t os::numa_get_groups_num() { 2244 return 1; 2245 } 2246 2247 int os::numa_get_group_id() { 2248 return 0; 2249 } 2250 2251 size_t os::numa_get_leaf_groups(int *ids, size_t size) { 2252 if (size > 0) { 2253 ids[0] = 0; 2254 return 1; 2255 } 2256 return 0; 2257 } 2258 2259 bool os::get_page_info(char *start, page_info* info) { 2260 return false; 2261 } 2262 2263 char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) { 2264 return end; 2265 } 2266 2267 // Flags for reserve_shmatted_memory: 2268 #define RESSHM_WISHADDR_OR_FAIL 1 2269 #define RESSHM_TRY_16M_PAGES 2 2270 #define RESSHM_16M_PAGES_OR_FAIL 4 2271 2272 // Result of reserve_shmatted_memory: 2273 struct shmatted_memory_info_t { 2274 char* addr; 2275 size_t pagesize; 2276 bool pinned; 2277 }; 2278 2279 // Reserve a section of shmatted memory. 2280 // params: 2281 // bytes [in]: size of memory, in bytes 2282 // requested_addr [in]: wish address. 2283 // NULL = no wish. 2284 // If RESSHM_WISHADDR_OR_FAIL is set in flags and wish address cannot 2285 // be obtained, function will fail. Otherwise wish address is treated as hint and 2286 // another pointer is returned. 2287 // flags [in]: some flags. Valid flags are: 2288 // RESSHM_WISHADDR_OR_FAIL - fail if wish address is given and cannot be obtained. 2289 // RESSHM_TRY_16M_PAGES - try to allocate from 16M page pool 2290 // (requires UseLargePages and Use16MPages) 2291 // RESSHM_16M_PAGES_OR_FAIL - if you cannot allocate from 16M page pool, fail. 2292 // Otherwise any other page size will do. 2293 // p_info [out] : holds information about the created shared memory segment. 2294 static bool reserve_shmatted_memory(size_t bytes, char* requested_addr, int flags, shmatted_memory_info_t* p_info) { 2295 2296 assert(p_info, "parameter error"); 2297 2298 // init output struct. 2299 p_info->addr = NULL; 2300 2301 // neither should we be here for EXTSHM=ON. 2302 if (os::Aix::extshm()) { 2303 ShouldNotReachHere(); 2304 } 2305 2306 // extract flags. sanity checks. 2307 const bool wishaddr_or_fail = 2308 flags & RESSHM_WISHADDR_OR_FAIL; 2309 const bool try_16M_pages = 2310 flags & RESSHM_TRY_16M_PAGES; 2311 const bool f16M_pages_or_fail = 2312 flags & RESSHM_16M_PAGES_OR_FAIL; 2313 2314 // first check: if a wish address is given and it is mandatory, but not aligned to segment boundary, 2315 // shmat will fail anyway, so save some cycles by failing right away 2316 if (requested_addr && ((uintptr_t)requested_addr % SIZE_256M == 0)) { 2317 if (wishaddr_or_fail) { 2318 return false; 2319 } else { 2320 requested_addr = NULL; 2321 } 2322 } 2323 2324 char* addr = NULL; 2325 2326 // Align size of shm up to the largest possible page size, to avoid errors later on when we try to change 2327 // pagesize dynamically. 2328 const size_t size = align_size_up(bytes, SIZE_16M); 2329 2330 // reserve the shared segment 2331 int shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | S_IRUSR | S_IWUSR); 2332 if (shmid == -1) { 2333 warning("shmget(.., %lld, ..) failed (errno: %d).", size, errno); 2334 return false; 2335 } 2336 2337 // Important note: 2338 // It is very important that we, upon leaving this function, do not leave a shm segment alive. 2339 // We must right after attaching it remove it from the system. System V shm segments are global and 2340 // survive the process. 2341 // So, from here on: Do not assert. Do not return. Always do a "goto cleanup_shm". 2342 2343 // try forcing the page size 2344 size_t pagesize = -1; // unknown so far 2345 2346 if (UseLargePages) { 2347 2348 struct shmid_ds shmbuf; 2349 memset(&shmbuf, 0, sizeof(shmbuf)); 2350 2351 // First, try to take from 16M page pool if... 2352 if (os::Aix::can_use_16M_pages() // we can ... 2353 && Use16MPages // we are not explicitly forbidden to do so (-XX:-Use16MPages).. 2354 && try_16M_pages) { // caller wants us to. 2355 shmbuf.shm_pagesize = SIZE_16M; 2356 if (shmctl(shmid, SHM_PAGESIZE, &shmbuf) == 0) { 2357 pagesize = SIZE_16M; 2358 } else { 2359 warning("Failed to allocate %d 16M pages. 16M page pool might be exhausted. (shmctl failed with %d)", 2360 size / SIZE_16M, errno); 2361 if (f16M_pages_or_fail) { 2362 goto cleanup_shm; 2363 } 2364 } 2365 } 2366 2367 // Nothing yet? Try setting 64K pages. Note that I never saw this fail, but in theory it might, 2368 // because the 64K page pool may also be exhausted. 2369 if (pagesize == -1) { 2370 shmbuf.shm_pagesize = SIZE_64K; 2371 if (shmctl(shmid, SHM_PAGESIZE, &shmbuf) == 0) { 2372 pagesize = SIZE_64K; 2373 } else { 2374 warning("Failed to allocate %d 64K pages. (shmctl failed with %d)", 2375 size / SIZE_64K, errno); 2376 // here I give up. leave page_size -1 - later, after attaching, we will query the 2377 // real page size of the attached memory. (in theory, it may be something different 2378 // from 4K if LDR_CNTRL SHM_PSIZE is set) 2379 } 2380 } 2381 } 2382 2383 // sanity point 2384 assert(pagesize == -1 || pagesize == SIZE_16M || pagesize == SIZE_64K, "wrong page size"); 2385 2386 // Now attach the shared segment. 2387 addr = (char*) shmat(shmid, requested_addr, 0); 2388 if (addr == (char*)-1) { 2389 // How to handle attach failure: 2390 // If it failed for a specific wish address, tolerate this: in that case, if wish address was 2391 // mandatory, fail, if not, retry anywhere. 2392 // If it failed for any other reason, treat that as fatal error. 2393 addr = NULL; 2394 if (requested_addr) { 2395 if (wishaddr_or_fail) { 2396 goto cleanup_shm; 2397 } else { 2398 addr = (char*) shmat(shmid, NULL, 0); 2399 if (addr == (char*)-1) { // fatal 2400 addr = NULL; 2401 warning("shmat failed (errno: %d)", errno); 2402 goto cleanup_shm; 2403 } 2404 } 2405 } else { // fatal 2406 addr = NULL; 2407 warning("shmat failed (errno: %d)", errno); 2408 goto cleanup_shm; 2409 } 2410 } 2411 2412 // sanity point 2413 assert(addr && addr != (char*) -1, "wrong address"); 2414 2415 // after successful Attach remove the segment - right away. 2416 if (::shmctl(shmid, IPC_RMID, NULL) == -1) { 2417 warning("shmctl(%u, IPC_RMID) failed (%d)\n", shmid, errno); 2418 guarantee(false, "failed to remove shared memory segment!"); 2419 } 2420 shmid = -1; 2421 2422 // query the real page size. In case setting the page size did not work (see above), the system 2423 // may have given us something other then 4K (LDR_CNTRL) 2424 { 2425 const size_t real_pagesize = os::Aix::query_pagesize(addr); 2426 if (pagesize != -1) { 2427 assert(pagesize == real_pagesize, "unexpected pagesize after shmat"); 2428 } else { 2429 pagesize = real_pagesize; 2430 } 2431 } 2432 2433 // Now register the reserved block with internal book keeping. 2434 LOCK_SHMBK 2435 const bool pinned = pagesize >= SIZE_16M ? true : false; 2436 ShmBkShmatedBlock* const p_block = new ShmBkShmatedBlock(AddrRange(addr, size), pagesize, pinned); 2437 assert(p_block, ""); 2438 shmbk_register(p_block); 2439 UNLOCK_SHMBK 2440 2441 cleanup_shm: 2442 2443 // if we have not done so yet, remove the shared memory segment. This is very important. 2444 if (shmid != -1) { 2445 if (::shmctl(shmid, IPC_RMID, NULL) == -1) { 2446 warning("shmctl(%u, IPC_RMID) failed (%d)\n", shmid, errno); 2447 guarantee(false, "failed to remove shared memory segment!"); 2448 } 2449 shmid = -1; 2450 } 2451 2452 // trace 2453 if (Verbose && !addr) { 2454 if (requested_addr != NULL) { 2455 warning("failed to shm-allocate 0x%llX bytes at wish address 0x%p.", size, requested_addr); 2456 } else { 2457 warning("failed to shm-allocate 0x%llX bytes at any address.", size); 2458 } 2459 } 2460 2461 // hand info to caller 2462 if (addr) { 2463 p_info->addr = addr; 2464 p_info->pagesize = pagesize; 2465 p_info->pinned = pagesize == SIZE_16M ? true : false; 2466 } 2467 2468 // sanity test: 2469 if (requested_addr && addr && wishaddr_or_fail) { 2470 guarantee(addr == requested_addr, "shmat error"); 2471 } 2472 2473 // just one more test to really make sure we have no dangling shm segments. 2474 guarantee(shmid == -1, "dangling shm segments"); 2475 2476 return addr ? true : false; 2477 2478 } // end: reserve_shmatted_memory 2479 2480 // Reserve memory using mmap. Behaves the same as reserve_shmatted_memory(): 2481 // will return NULL in case of an error. 2482 static char* reserve_mmaped_memory(size_t bytes, char* requested_addr) { 2483 2484 // if a wish address is given, but not aligned to 4K page boundary, mmap will fail. 2485 if (requested_addr && ((uintptr_t)requested_addr % os::vm_page_size() != 0)) { 2486 warning("Wish address 0x%p not aligned to page boundary.", requested_addr); 2487 return NULL; 2488 } 2489 2490 const size_t size = align_size_up(bytes, SIZE_4K); 2491 2492 // Note: MAP_SHARED (instead of MAP_PRIVATE) needed to be able to 2493 // msync(MS_INVALIDATE) (see os::uncommit_memory) 2494 int flags = MAP_ANONYMOUS | MAP_SHARED; 2495 2496 // MAP_FIXED is needed to enforce requested_addr - manpage is vague about what 2497 // it means if wishaddress is given but MAP_FIXED is not set. 2498 // 2499 // Note however that this changes semantics in SPEC1170 mode insofar as MAP_FIXED 2500 // clobbers the address range, which is probably not what the caller wants. That's 2501 // why I assert here (again) that the SPEC1170 compat mode is off. 2502 // If we want to be able to run under SPEC1170, we have to do some porting and 2503 // testing. 2504 if (requested_addr != NULL) { 2505 assert(!os::Aix::xpg_sus_mode(), "SPEC1170 mode not allowed."); 2506 flags |= MAP_FIXED; 2507 } 2508 2509 char* addr = (char*)::mmap(requested_addr, size, PROT_READ|PROT_WRITE|PROT_EXEC, flags, -1, 0); 2510 2511 if (addr == MAP_FAILED) { 2512 // attach failed: tolerate for specific wish addresses. Not being able to attach 2513 // anywhere is a fatal error. 2514 if (requested_addr == NULL) { 2515 // It's ok to fail here if the machine has not enough memory. 2516 warning("mmap(NULL, 0x%llX, ..) failed (%d)", size, errno); 2517 } 2518 addr = NULL; 2519 goto cleanup_mmap; 2520 } 2521 2522 // If we did request a specific address and that address was not available, fail. 2523 if (addr && requested_addr) { 2524 guarantee(addr == requested_addr, "unexpected"); 2525 } 2526 2527 // register this mmap'ed segment with book keeping 2528 LOCK_SHMBK 2529 ShmBkMappedBlock* const p_block = new ShmBkMappedBlock(AddrRange(addr, size)); 2530 assert(p_block, ""); 2531 shmbk_register(p_block); 2532 UNLOCK_SHMBK 2533 2534 cleanup_mmap: 2535 2536 // trace 2537 if (Verbose) { 2538 if (addr) { 2539 fprintf(stderr, "mmap-allocated 0x%p .. 0x%p (0x%llX bytes)\n", addr, addr + bytes, bytes); 2540 } 2541 else { 2542 if (requested_addr != NULL) { 2543 warning("failed to mmap-allocate 0x%llX bytes at wish address 0x%p.", bytes, requested_addr); 2544 } else { 2545 warning("failed to mmap-allocate 0x%llX bytes at any address.", bytes); 2546 } 2547 } 2548 } 2549 2550 return addr; 2551 2552 } // end: reserve_mmaped_memory 2553 2554 // Reserves and attaches a shared memory segment. 2555 // Will assert if a wish address is given and could not be obtained. 2556 char* os::pd_reserve_memory(size_t bytes, char* requested_addr, size_t alignment_hint) { 2557 return os::attempt_reserve_memory_at(bytes, requested_addr); 2558 } 2559 2560 bool os::pd_release_memory(char* addr, size_t size) { 2561 2562 // delegate to ShmBkBlock class which knows how to uncommit its memory. 2563 2564 bool rc = false; 2565 LOCK_SHMBK 2566 ShmBkBlock* const block = shmbk_find_by_containing_address(addr); 2567 if (!block) { 2568 fprintf(stderr, "invalid pointer: 0x%p.\n", addr); 2569 shmbk_dump_info(); 2570 assert(false, "invalid pointer"); 2571 return false; 2572 } 2573 else if (!block->isSameRange(addr, size)) { 2574 if (block->getType() == ShmBkBlock::MMAP) { 2575 // Release only the same range or a the beginning or the end of a range. 2576 if (block->base() == addr && size < block->size()) { 2577 ShmBkMappedBlock* const b = new ShmBkMappedBlock(AddrRange(block->base() + size, block->size() - size)); 2578 assert(b, ""); 2579 shmbk_register(b); 2580 block->setAddrRange(AddrRange(addr, size)); 2581 } 2582 else if (addr > block->base() && addr + size == block->base() + block->size()) { 2583 ShmBkMappedBlock* const b = new ShmBkMappedBlock(AddrRange(block->base(), block->size() - size)); 2584 assert(b, ""); 2585 shmbk_register(b); 2586 block->setAddrRange(AddrRange(addr, size)); 2587 } 2588 else { 2589 fprintf(stderr, "invalid mmap range: 0x%p .. 0x%p.\n", addr, addr + size); 2590 shmbk_dump_info(); 2591 assert(false, "invalid mmap range"); 2592 return false; 2593 } 2594 } 2595 else { 2596 // Release only the same range. No partial release allowed. 2597 // Soften the requirement a bit, because the user may think he owns a smaller size 2598 // than the block is due to alignment etc. 2599 if (block->base() != addr || block->size() < size) { 2600 fprintf(stderr, "invalid shmget range: 0x%p .. 0x%p.\n", addr, addr + size); 2601 shmbk_dump_info(); 2602 assert(false, "invalid shmget range"); 2603 return false; 2604 } 2605 } 2606 } 2607 rc = block->release(); 2608 assert(rc, "release failed"); 2609 // remove block from bookkeeping 2610 shmbk_unregister(block); 2611 delete block; 2612 UNLOCK_SHMBK 2613 2614 if (!rc) { 2615 warning("failed to released %lu bytes at 0x%p", size, addr); 2616 } 2617 2618 return rc; 2619 } 2620 2621 static bool checked_mprotect(char* addr, size_t size, int prot) { 2622 2623 // Little problem here: if SPEC1170 behaviour is off, mprotect() on AIX will 2624 // not tell me if protection failed when trying to protect an un-protectable range. 2625 // 2626 // This means if the memory was allocated using shmget/shmat, protection wont work 2627 // but mprotect will still return 0: 2628 // 2629 // See http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/mprotect.htm 2630 2631 bool rc = ::mprotect(addr, size, prot) == 0 ? true : false; 2632 2633 if (!rc) { 2634 const char* const s_errno = strerror(errno); 2635 warning("mprotect(" PTR_FORMAT "-" PTR_FORMAT ", 0x%X) failed (%s).", addr, addr + size, prot, s_errno); 2636 return false; 2637 } 2638 2639 // mprotect success check 2640 // 2641 // Mprotect said it changed the protection but can I believe it? 2642 // 2643 // To be sure I need to check the protection afterwards. Try to 2644 // read from protected memory and check whether that causes a segfault. 2645 // 2646 if (!os::Aix::xpg_sus_mode()) { 2647 2648 if (StubRoutines::SafeFetch32_stub()) { 2649 2650 const bool read_protected = 2651 (SafeFetch32((int*)addr, 0x12345678) == 0x12345678 && 2652 SafeFetch32((int*)addr, 0x76543210) == 0x76543210) ? true : false; 2653 2654 if (prot & PROT_READ) { 2655 rc = !read_protected; 2656 } else { 2657 rc = read_protected; 2658 } 2659 } 2660 } 2661 if (!rc) { 2662 assert(false, "mprotect failed."); 2663 } 2664 return rc; 2665 } 2666 2667 // Set protections specified 2668 bool os::protect_memory(char* addr, size_t size, ProtType prot, bool is_committed) { 2669 unsigned int p = 0; 2670 switch (prot) { 2671 case MEM_PROT_NONE: p = PROT_NONE; break; 2672 case MEM_PROT_READ: p = PROT_READ; break; 2673 case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break; 2674 case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break; 2675 default: 2676 ShouldNotReachHere(); 2677 } 2678 // is_committed is unused. 2679 return checked_mprotect(addr, size, p); 2680 } 2681 2682 bool os::guard_memory(char* addr, size_t size) { 2683 return checked_mprotect(addr, size, PROT_NONE); 2684 } 2685 2686 bool os::unguard_memory(char* addr, size_t size) { 2687 return checked_mprotect(addr, size, PROT_READ|PROT_WRITE|PROT_EXEC); 2688 } 2689 2690 // Large page support 2691 2692 static size_t _large_page_size = 0; 2693 2694 // Enable large page support if OS allows that. 2695 void os::large_page_init() { 2696 2697 // Note: os::Aix::query_multipage_support must run first. 2698 2699 if (!UseLargePages) { 2700 return; 2701 } 2702 2703 if (!Aix::can_use_64K_pages()) { 2704 assert(!Aix::can_use_16M_pages(), "64K is a precondition for 16M."); 2705 UseLargePages = false; 2706 return; 2707 } 2708 2709 if (!Aix::can_use_16M_pages() && Use16MPages) { 2710 fprintf(stderr, "Cannot use 16M pages. Please ensure that there is a 16M page pool " 2711 " and that the VM runs with CAP_BYPASS_RAC_VMM and CAP_PROPAGATE capabilities.\n"); 2712 } 2713 2714 // Do not report 16M page alignment as part of os::_page_sizes if we are 2715 // explicitly forbidden from using 16M pages. Doing so would increase the 2716 // alignment the garbage collector calculates with, slightly increasing 2717 // heap usage. We should only pay for 16M alignment if we really want to 2718 // use 16M pages. 2719 if (Use16MPages && Aix::can_use_16M_pages()) { 2720 _large_page_size = SIZE_16M; 2721 _page_sizes[0] = SIZE_16M; 2722 _page_sizes[1] = SIZE_64K; 2723 _page_sizes[2] = SIZE_4K; 2724 _page_sizes[3] = 0; 2725 } else if (Aix::can_use_64K_pages()) { 2726 _large_page_size = SIZE_64K; 2727 _page_sizes[0] = SIZE_64K; 2728 _page_sizes[1] = SIZE_4K; 2729 _page_sizes[2] = 0; 2730 } 2731 2732 if (Verbose) { 2733 ("Default large page size is 0x%llX.", _large_page_size); 2734 } 2735 } // end: os::large_page_init() 2736 2737 char* os::reserve_memory_special(size_t bytes, size_t alignment, char* req_addr, bool exec) { 2738 // "exec" is passed in but not used. Creating the shared image for 2739 // the code cache doesn't have an SHM_X executable permission to check. 2740 Unimplemented(); 2741 return 0; 2742 } 2743 2744 bool os::release_memory_special(char* base, size_t bytes) { 2745 // detaching the SHM segment will also delete it, see reserve_memory_special() 2746 Unimplemented(); 2747 return false; 2748 } 2749 2750 size_t os::large_page_size() { 2751 return _large_page_size; 2752 } 2753 2754 bool os::can_commit_large_page_memory() { 2755 // Well, sadly we cannot commit anything at all (see comment in 2756 // os::commit_memory) but we claim to so we can make use of large pages 2757 return true; 2758 } 2759 2760 bool os::can_execute_large_page_memory() { 2761 // We can do that 2762 return true; 2763 } 2764 2765 // Reserve memory at an arbitrary address, only if that area is 2766 // available (and not reserved for something else). 2767 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) { 2768 2769 bool use_mmap = false; 2770 2771 // mmap: smaller graining, no large page support 2772 // shm: large graining (256M), large page support, limited number of shm segments 2773 // 2774 // Prefer mmap wherever we either do not need large page support or have OS limits 2775 2776 if (!UseLargePages || bytes < SIZE_16M) { 2777 use_mmap = true; 2778 } 2779 2780 char* addr = NULL; 2781 if (use_mmap) { 2782 addr = reserve_mmaped_memory(bytes, requested_addr); 2783 } else { 2784 // shmat: wish address is mandatory, and do not try 16M pages here. 2785 shmatted_memory_info_t info; 2786 const int flags = RESSHM_WISHADDR_OR_FAIL; 2787 if (reserve_shmatted_memory(bytes, requested_addr, flags, &info)) { 2788 addr = info.addr; 2789 } 2790 } 2791 2792 return addr; 2793 } 2794 2795 size_t os::read(int fd, void *buf, unsigned int nBytes) { 2796 return ::read(fd, buf, nBytes); 2797 } 2798 2799 size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) { 2800 return ::pread(fd, buf, nBytes, offset); 2801 } 2802 2803 void os::naked_short_sleep(jlong ms) { 2804 struct timespec req; 2805 2806 assert(ms < 1000, "Un-interruptable sleep, short time use only"); 2807 req.tv_sec = 0; 2808 if (ms > 0) { 2809 req.tv_nsec = (ms % 1000) * 1000000; 2810 } 2811 else { 2812 req.tv_nsec = 1; 2813 } 2814 2815 nanosleep(&req, NULL); 2816 2817 return; 2818 } 2819 2820 // Sleep forever; naked call to OS-specific sleep; use with CAUTION 2821 void os::infinite_sleep() { 2822 while (true) { // sleep forever ... 2823 ::sleep(100); // ... 100 seconds at a time 2824 } 2825 } 2826 2827 // Used to convert frequent JVM_Yield() to nops 2828 bool os::dont_yield() { 2829 return DontYieldALot; 2830 } 2831 2832 void os::naked_yield() { 2833 sched_yield(); 2834 } 2835 2836 //////////////////////////////////////////////////////////////////////////////// 2837 // thread priority support 2838 2839 // From AIX manpage to pthread_setschedparam 2840 // (see: http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp? 2841 // topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_setschedparam.htm): 2842 // 2843 // "If schedpolicy is SCHED_OTHER, then sched_priority must be in the 2844 // range from 40 to 80, where 40 is the least favored priority and 80 2845 // is the most favored." 2846 // 2847 // (Actually, I doubt this even has an impact on AIX, as we do kernel 2848 // scheduling there; however, this still leaves iSeries.) 2849 // 2850 // We use the same values for AIX and PASE. 2851 int os::java_to_os_priority[CriticalPriority + 1] = { 2852 54, // 0 Entry should never be used 2853 2854 55, // 1 MinPriority 2855 55, // 2 2856 56, // 3 2857 2858 56, // 4 2859 57, // 5 NormPriority 2860 57, // 6 2861 2862 58, // 7 2863 58, // 8 2864 59, // 9 NearMaxPriority 2865 2866 60, // 10 MaxPriority 2867 2868 60 // 11 CriticalPriority 2869 }; 2870 2871 OSReturn os::set_native_priority(Thread* thread, int newpri) { 2872 if (!UseThreadPriorities) return OS_OK; 2873 pthread_t thr = thread->osthread()->pthread_id(); 2874 int policy = SCHED_OTHER; 2875 struct sched_param param; 2876 param.sched_priority = newpri; 2877 int ret = pthread_setschedparam(thr, policy, ¶m); 2878 2879 if (ret != 0) { 2880 trcVerbose("Could not change priority for thread %d to %d (error %d, %s)", 2881 (int)thr, newpri, ret, strerror(ret)); 2882 } 2883 return (ret == 0) ? OS_OK : OS_ERR; 2884 } 2885 2886 OSReturn os::get_native_priority(const Thread* const thread, int *priority_ptr) { 2887 if (!UseThreadPriorities) { 2888 *priority_ptr = java_to_os_priority[NormPriority]; 2889 return OS_OK; 2890 } 2891 pthread_t thr = thread->osthread()->pthread_id(); 2892 int policy = SCHED_OTHER; 2893 struct sched_param param; 2894 int ret = pthread_getschedparam(thr, &policy, ¶m); 2895 *priority_ptr = param.sched_priority; 2896 2897 return (ret == 0) ? OS_OK : OS_ERR; 2898 } 2899 2900 // Hint to the underlying OS that a task switch would not be good. 2901 // Void return because it's a hint and can fail. 2902 void os::hint_no_preempt() {} 2903 2904 //////////////////////////////////////////////////////////////////////////////// 2905 // suspend/resume support 2906 2907 // the low-level signal-based suspend/resume support is a remnant from the 2908 // old VM-suspension that used to be for java-suspension, safepoints etc, 2909 // within hotspot. Now there is a single use-case for this: 2910 // - calling get_thread_pc() on the VMThread by the flat-profiler task 2911 // that runs in the watcher thread. 2912 // The remaining code is greatly simplified from the more general suspension 2913 // code that used to be used. 2914 // 2915 // The protocol is quite simple: 2916 // - suspend: 2917 // - sends a signal to the target thread 2918 // - polls the suspend state of the osthread using a yield loop 2919 // - target thread signal handler (SR_handler) sets suspend state 2920 // and blocks in sigsuspend until continued 2921 // - resume: 2922 // - sets target osthread state to continue 2923 // - sends signal to end the sigsuspend loop in the SR_handler 2924 // 2925 // Note that the SR_lock plays no role in this suspend/resume protocol. 2926 // 2927 2928 static void resume_clear_context(OSThread *osthread) { 2929 osthread->set_ucontext(NULL); 2930 osthread->set_siginfo(NULL); 2931 } 2932 2933 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) { 2934 osthread->set_ucontext(context); 2935 osthread->set_siginfo(siginfo); 2936 } 2937 2938 // 2939 // Handler function invoked when a thread's execution is suspended or 2940 // resumed. We have to be careful that only async-safe functions are 2941 // called here (Note: most pthread functions are not async safe and 2942 // should be avoided.) 2943 // 2944 // Note: sigwait() is a more natural fit than sigsuspend() from an 2945 // interface point of view, but sigwait() prevents the signal hander 2946 // from being run. libpthread would get very confused by not having 2947 // its signal handlers run and prevents sigwait()'s use with the 2948 // mutex granting granting signal. 2949 // 2950 // Currently only ever called on the VMThread and JavaThreads (PC sampling). 2951 // 2952 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) { 2953 // Save and restore errno to avoid confusing native code with EINTR 2954 // after sigsuspend. 2955 int old_errno = errno; 2956 2957 Thread* thread = Thread::current(); 2958 OSThread* osthread = thread->osthread(); 2959 assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread"); 2960 2961 os::SuspendResume::State current = osthread->sr.state(); 2962 if (current == os::SuspendResume::SR_SUSPEND_REQUEST) { 2963 suspend_save_context(osthread, siginfo, context); 2964 2965 // attempt to switch the state, we assume we had a SUSPEND_REQUEST 2966 os::SuspendResume::State state = osthread->sr.suspended(); 2967 if (state == os::SuspendResume::SR_SUSPENDED) { 2968 sigset_t suspend_set; // signals for sigsuspend() 2969 2970 // get current set of blocked signals and unblock resume signal 2971 pthread_sigmask(SIG_BLOCK, NULL, &suspend_set); 2972 sigdelset(&suspend_set, SR_signum); 2973 2974 // wait here until we are resumed 2975 while (1) { 2976 sigsuspend(&suspend_set); 2977 2978 os::SuspendResume::State result = osthread->sr.running(); 2979 if (result == os::SuspendResume::SR_RUNNING) { 2980 break; 2981 } 2982 } 2983 2984 } else if (state == os::SuspendResume::SR_RUNNING) { 2985 // request was cancelled, continue 2986 } else { 2987 ShouldNotReachHere(); 2988 } 2989 2990 resume_clear_context(osthread); 2991 } else if (current == os::SuspendResume::SR_RUNNING) { 2992 // request was cancelled, continue 2993 } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) { 2994 // ignore 2995 } else { 2996 ShouldNotReachHere(); 2997 } 2998 2999 errno = old_errno; 3000 } 3001 3002 static int SR_initialize() { 3003 struct sigaction act; 3004 char *s; 3005 // Get signal number to use for suspend/resume 3006 if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) { 3007 int sig = ::strtol(s, 0, 10); 3008 if (sig > 0 || sig < NSIG) { 3009 SR_signum = sig; 3010 } 3011 } 3012 3013 assert(SR_signum > SIGSEGV && SR_signum > SIGBUS, 3014 "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769"); 3015 3016 sigemptyset(&SR_sigset); 3017 sigaddset(&SR_sigset, SR_signum); 3018 3019 // Set up signal handler for suspend/resume. 3020 act.sa_flags = SA_RESTART|SA_SIGINFO; 3021 act.sa_handler = (void (*)(int)) SR_handler; 3022 3023 // SR_signum is blocked by default. 3024 // 4528190 - We also need to block pthread restart signal (32 on all 3025 // supported Linux platforms). Note that LinuxThreads need to block 3026 // this signal for all threads to work properly. So we don't have 3027 // to use hard-coded signal number when setting up the mask. 3028 pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask); 3029 3030 if (sigaction(SR_signum, &act, 0) == -1) { 3031 return -1; 3032 } 3033 3034 // Save signal flag 3035 os::Aix::set_our_sigflags(SR_signum, act.sa_flags); 3036 return 0; 3037 } 3038 3039 static int SR_finalize() { 3040 return 0; 3041 } 3042 3043 static int sr_notify(OSThread* osthread) { 3044 int status = pthread_kill(osthread->pthread_id(), SR_signum); 3045 assert_status(status == 0, status, "pthread_kill"); 3046 return status; 3047 } 3048 3049 // "Randomly" selected value for how long we want to spin 3050 // before bailing out on suspending a thread, also how often 3051 // we send a signal to a thread we want to resume 3052 static const int RANDOMLY_LARGE_INTEGER = 1000000; 3053 static const int RANDOMLY_LARGE_INTEGER2 = 100; 3054 3055 // returns true on success and false on error - really an error is fatal 3056 // but this seems the normal response to library errors 3057 static bool do_suspend(OSThread* osthread) { 3058 assert(osthread->sr.is_running(), "thread should be running"); 3059 // mark as suspended and send signal 3060 3061 if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) { 3062 // failed to switch, state wasn't running? 3063 ShouldNotReachHere(); 3064 return false; 3065 } 3066 3067 if (sr_notify(osthread) != 0) { 3068 // try to cancel, switch to running 3069 3070 os::SuspendResume::State result = osthread->sr.cancel_suspend(); 3071 if (result == os::SuspendResume::SR_RUNNING) { 3072 // cancelled 3073 return false; 3074 } else if (result == os::SuspendResume::SR_SUSPENDED) { 3075 // somehow managed to suspend 3076 return true; 3077 } else { 3078 ShouldNotReachHere(); 3079 return false; 3080 } 3081 } 3082 3083 // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED 3084 3085 for (int n = 0; !osthread->sr.is_suspended(); n++) { 3086 for (int i = 0; i < RANDOMLY_LARGE_INTEGER2 && !osthread->sr.is_suspended(); i++) { 3087 os::naked_yield(); 3088 } 3089 3090 // timeout, try to cancel the request 3091 if (n >= RANDOMLY_LARGE_INTEGER) { 3092 os::SuspendResume::State cancelled = osthread->sr.cancel_suspend(); 3093 if (cancelled == os::SuspendResume::SR_RUNNING) { 3094 return false; 3095 } else if (cancelled == os::SuspendResume::SR_SUSPENDED) { 3096 return true; 3097 } else { 3098 ShouldNotReachHere(); 3099 return false; 3100 } 3101 } 3102 } 3103 3104 guarantee(osthread->sr.is_suspended(), "Must be suspended"); 3105 return true; 3106 } 3107 3108 static void do_resume(OSThread* osthread) { 3109 //assert(osthread->sr.is_suspended(), "thread should be suspended"); 3110 3111 if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) { 3112 // failed to switch to WAKEUP_REQUEST 3113 ShouldNotReachHere(); 3114 return; 3115 } 3116 3117 while (!osthread->sr.is_running()) { 3118 if (sr_notify(osthread) == 0) { 3119 for (int n = 0; n < RANDOMLY_LARGE_INTEGER && !osthread->sr.is_running(); n++) { 3120 for (int i = 0; i < 100 && !osthread->sr.is_running(); i++) { 3121 os::naked_yield(); 3122 } 3123 } 3124 } else { 3125 ShouldNotReachHere(); 3126 } 3127 } 3128 3129 guarantee(osthread->sr.is_running(), "Must be running!"); 3130 } 3131 3132 /////////////////////////////////////////////////////////////////////////////////// 3133 // signal handling (except suspend/resume) 3134 3135 // This routine may be used by user applications as a "hook" to catch signals. 3136 // The user-defined signal handler must pass unrecognized signals to this 3137 // routine, and if it returns true (non-zero), then the signal handler must 3138 // return immediately. If the flag "abort_if_unrecognized" is true, then this 3139 // routine will never retun false (zero), but instead will execute a VM panic 3140 // routine kill the process. 3141 // 3142 // If this routine returns false, it is OK to call it again. This allows 3143 // the user-defined signal handler to perform checks either before or after 3144 // the VM performs its own checks. Naturally, the user code would be making 3145 // a serious error if it tried to handle an exception (such as a null check 3146 // or breakpoint) that the VM was generating for its own correct operation. 3147 // 3148 // This routine may recognize any of the following kinds of signals: 3149 // SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1. 3150 // It should be consulted by handlers for any of those signals. 3151 // 3152 // The caller of this routine must pass in the three arguments supplied 3153 // to the function referred to in the "sa_sigaction" (not the "sa_handler") 3154 // field of the structure passed to sigaction(). This routine assumes that 3155 // the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART. 3156 // 3157 // Note that the VM will print warnings if it detects conflicting signal 3158 // handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers". 3159 // 3160 extern "C" JNIEXPORT int 3161 JVM_handle_aix_signal(int signo, siginfo_t* siginfo, void* ucontext, int abort_if_unrecognized); 3162 3163 // Set thread signal mask (for some reason on AIX sigthreadmask() seems 3164 // to be the thing to call; documentation is not terribly clear about whether 3165 // pthread_sigmask also works, and if it does, whether it does the same. 3166 bool set_thread_signal_mask(int how, const sigset_t* set, sigset_t* oset) { 3167 const int rc = ::pthread_sigmask(how, set, oset); 3168 // return value semantics differ slightly for error case: 3169 // pthread_sigmask returns error number, sigthreadmask -1 and sets global errno 3170 // (so, pthread_sigmask is more theadsafe for error handling) 3171 // But success is always 0. 3172 return rc == 0 ? true : false; 3173 } 3174 3175 // Function to unblock all signals which are, according 3176 // to POSIX, typical program error signals. If they happen while being blocked, 3177 // they typically will bring down the process immediately. 3178 bool unblock_program_error_signals() { 3179 sigset_t set; 3180 ::sigemptyset(&set); 3181 ::sigaddset(&set, SIGILL); 3182 ::sigaddset(&set, SIGBUS); 3183 ::sigaddset(&set, SIGFPE); 3184 ::sigaddset(&set, SIGSEGV); 3185 return set_thread_signal_mask(SIG_UNBLOCK, &set, NULL); 3186 } 3187 3188 // Renamed from 'signalHandler' to avoid collision with other shared libs. 3189 void javaSignalHandler(int sig, siginfo_t* info, void* uc) { 3190 assert(info != NULL && uc != NULL, "it must be old kernel"); 3191 3192 // Never leave program error signals blocked; 3193 // on all our platforms they would bring down the process immediately when 3194 // getting raised while being blocked. 3195 unblock_program_error_signals(); 3196 3197 JVM_handle_aix_signal(sig, info, uc, true); 3198 } 3199 3200 // This boolean allows users to forward their own non-matching signals 3201 // to JVM_handle_aix_signal, harmlessly. 3202 bool os::Aix::signal_handlers_are_installed = false; 3203 3204 // For signal-chaining 3205 struct sigaction os::Aix::sigact[MAXSIGNUM]; 3206 unsigned int os::Aix::sigs = 0; 3207 bool os::Aix::libjsig_is_loaded = false; 3208 typedef struct sigaction *(*get_signal_t)(int); 3209 get_signal_t os::Aix::get_signal_action = NULL; 3210 3211 struct sigaction* os::Aix::get_chained_signal_action(int sig) { 3212 struct sigaction *actp = NULL; 3213 3214 if (libjsig_is_loaded) { 3215 // Retrieve the old signal handler from libjsig 3216 actp = (*get_signal_action)(sig); 3217 } 3218 if (actp == NULL) { 3219 // Retrieve the preinstalled signal handler from jvm 3220 actp = get_preinstalled_handler(sig); 3221 } 3222 3223 return actp; 3224 } 3225 3226 static bool call_chained_handler(struct sigaction *actp, int sig, 3227 siginfo_t *siginfo, void *context) { 3228 // Call the old signal handler 3229 if (actp->sa_handler == SIG_DFL) { 3230 // It's more reasonable to let jvm treat it as an unexpected exception 3231 // instead of taking the default action. 3232 return false; 3233 } else if (actp->sa_handler != SIG_IGN) { 3234 if ((actp->sa_flags & SA_NODEFER) == 0) { 3235 // automaticlly block the signal 3236 sigaddset(&(actp->sa_mask), sig); 3237 } 3238 3239 sa_handler_t hand = NULL; 3240 sa_sigaction_t sa = NULL; 3241 bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0; 3242 // retrieve the chained handler 3243 if (siginfo_flag_set) { 3244 sa = actp->sa_sigaction; 3245 } else { 3246 hand = actp->sa_handler; 3247 } 3248 3249 if ((actp->sa_flags & SA_RESETHAND) != 0) { 3250 actp->sa_handler = SIG_DFL; 3251 } 3252 3253 // try to honor the signal mask 3254 sigset_t oset; 3255 pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset); 3256 3257 // call into the chained handler 3258 if (siginfo_flag_set) { 3259 (*sa)(sig, siginfo, context); 3260 } else { 3261 (*hand)(sig); 3262 } 3263 3264 // restore the signal mask 3265 pthread_sigmask(SIG_SETMASK, &oset, 0); 3266 } 3267 // Tell jvm's signal handler the signal is taken care of. 3268 return true; 3269 } 3270 3271 bool os::Aix::chained_handler(int sig, siginfo_t* siginfo, void* context) { 3272 bool chained = false; 3273 // signal-chaining 3274 if (UseSignalChaining) { 3275 struct sigaction *actp = get_chained_signal_action(sig); 3276 if (actp != NULL) { 3277 chained = call_chained_handler(actp, sig, siginfo, context); 3278 } 3279 } 3280 return chained; 3281 } 3282 3283 struct sigaction* os::Aix::get_preinstalled_handler(int sig) { 3284 if ((((unsigned int)1 << sig) & sigs) != 0) { 3285 return &sigact[sig]; 3286 } 3287 return NULL; 3288 } 3289 3290 void os::Aix::save_preinstalled_handler(int sig, struct sigaction& oldAct) { 3291 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range"); 3292 sigact[sig] = oldAct; 3293 sigs |= (unsigned int)1 << sig; 3294 } 3295 3296 // for diagnostic 3297 int os::Aix::sigflags[MAXSIGNUM]; 3298 3299 int os::Aix::get_our_sigflags(int sig) { 3300 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range"); 3301 return sigflags[sig]; 3302 } 3303 3304 void os::Aix::set_our_sigflags(int sig, int flags) { 3305 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range"); 3306 sigflags[sig] = flags; 3307 } 3308 3309 void os::Aix::set_signal_handler(int sig, bool set_installed) { 3310 // Check for overwrite. 3311 struct sigaction oldAct; 3312 sigaction(sig, (struct sigaction*)NULL, &oldAct); 3313 3314 void* oldhand = oldAct.sa_sigaction 3315 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 3316 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 3317 // Renamed 'signalHandler' to avoid collision with other shared libs. 3318 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) && 3319 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) && 3320 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)javaSignalHandler)) { 3321 if (AllowUserSignalHandlers || !set_installed) { 3322 // Do not overwrite; user takes responsibility to forward to us. 3323 return; 3324 } else if (UseSignalChaining) { 3325 // save the old handler in jvm 3326 save_preinstalled_handler(sig, oldAct); 3327 // libjsig also interposes the sigaction() call below and saves the 3328 // old sigaction on it own. 3329 } else { 3330 fatal(err_msg("Encountered unexpected pre-existing sigaction handler " 3331 "%#lx for signal %d.", (long)oldhand, sig)); 3332 } 3333 } 3334 3335 struct sigaction sigAct; 3336 sigfillset(&(sigAct.sa_mask)); 3337 if (!set_installed) { 3338 sigAct.sa_handler = SIG_DFL; 3339 sigAct.sa_flags = SA_RESTART; 3340 } else { 3341 // Renamed 'signalHandler' to avoid collision with other shared libs. 3342 sigAct.sa_sigaction = javaSignalHandler; 3343 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 3344 } 3345 // Save flags, which are set by ours 3346 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range"); 3347 sigflags[sig] = sigAct.sa_flags; 3348 3349 int ret = sigaction(sig, &sigAct, &oldAct); 3350 assert(ret == 0, "check"); 3351 3352 void* oldhand2 = oldAct.sa_sigaction 3353 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 3354 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 3355 assert(oldhand2 == oldhand, "no concurrent signal handler installation"); 3356 } 3357 3358 // install signal handlers for signals that HotSpot needs to 3359 // handle in order to support Java-level exception handling. 3360 void os::Aix::install_signal_handlers() { 3361 if (!signal_handlers_are_installed) { 3362 signal_handlers_are_installed = true; 3363 3364 // signal-chaining 3365 typedef void (*signal_setting_t)(); 3366 signal_setting_t begin_signal_setting = NULL; 3367 signal_setting_t end_signal_setting = NULL; 3368 begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t, 3369 dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting")); 3370 if (begin_signal_setting != NULL) { 3371 end_signal_setting = CAST_TO_FN_PTR(signal_setting_t, 3372 dlsym(RTLD_DEFAULT, "JVM_end_signal_setting")); 3373 get_signal_action = CAST_TO_FN_PTR(get_signal_t, 3374 dlsym(RTLD_DEFAULT, "JVM_get_signal_action")); 3375 libjsig_is_loaded = true; 3376 assert(UseSignalChaining, "should enable signal-chaining"); 3377 } 3378 if (libjsig_is_loaded) { 3379 // Tell libjsig jvm is setting signal handlers 3380 (*begin_signal_setting)(); 3381 } 3382 3383 set_signal_handler(SIGSEGV, true); 3384 set_signal_handler(SIGPIPE, true); 3385 set_signal_handler(SIGBUS, true); 3386 set_signal_handler(SIGILL, true); 3387 set_signal_handler(SIGFPE, true); 3388 set_signal_handler(SIGTRAP, true); 3389 set_signal_handler(SIGXFSZ, true); 3390 set_signal_handler(SIGDANGER, true); 3391 3392 if (libjsig_is_loaded) { 3393 // Tell libjsig jvm finishes setting signal handlers. 3394 (*end_signal_setting)(); 3395 } 3396 3397 // We don't activate signal checker if libjsig is in place, we trust ourselves 3398 // and if UserSignalHandler is installed all bets are off. 3399 // Log that signal checking is off only if -verbose:jni is specified. 3400 if (CheckJNICalls) { 3401 if (libjsig_is_loaded) { 3402 tty->print_cr("Info: libjsig is activated, all active signal checking is disabled"); 3403 check_signals = false; 3404 } 3405 if (AllowUserSignalHandlers) { 3406 tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled"); 3407 check_signals = false; 3408 } 3409 // Need to initialize check_signal_done. 3410 ::sigemptyset(&check_signal_done); 3411 } 3412 } 3413 } 3414 3415 static const char* get_signal_handler_name(address handler, 3416 char* buf, int buflen) { 3417 int offset; 3418 bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset); 3419 if (found) { 3420 // skip directory names 3421 const char *p1, *p2; 3422 p1 = buf; 3423 size_t len = strlen(os::file_separator()); 3424 while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len; 3425 // The way os::dll_address_to_library_name is implemented on Aix 3426 // right now, it always returns -1 for the offset which is not 3427 // terribly informative. 3428 // Will fix that. For now, omit the offset. 3429 jio_snprintf(buf, buflen, "%s", p1); 3430 } else { 3431 jio_snprintf(buf, buflen, PTR_FORMAT, handler); 3432 } 3433 return buf; 3434 } 3435 3436 static void print_signal_handler(outputStream* st, int sig, 3437 char* buf, size_t buflen) { 3438 struct sigaction sa; 3439 sigaction(sig, NULL, &sa); 3440 3441 st->print("%s: ", os::exception_name(sig, buf, buflen)); 3442 3443 address handler = (sa.sa_flags & SA_SIGINFO) 3444 ? CAST_FROM_FN_PTR(address, sa.sa_sigaction) 3445 : CAST_FROM_FN_PTR(address, sa.sa_handler); 3446 3447 if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) { 3448 st->print("SIG_DFL"); 3449 } else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) { 3450 st->print("SIG_IGN"); 3451 } else { 3452 st->print("[%s]", get_signal_handler_name(handler, buf, buflen)); 3453 } 3454 3455 // Print readable mask. 3456 st->print(", sa_mask[0]="); 3457 os::Posix::print_signal_set_short(st, &sa.sa_mask); 3458 3459 address rh = VMError::get_resetted_sighandler(sig); 3460 // May be, handler was resetted by VMError? 3461 if (rh != NULL) { 3462 handler = rh; 3463 sa.sa_flags = VMError::get_resetted_sigflags(sig); 3464 } 3465 3466 // Print textual representation of sa_flags. 3467 st->print(", sa_flags="); 3468 os::Posix::print_sa_flags(st, sa.sa_flags); 3469 3470 // Check: is it our handler? 3471 if (handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)javaSignalHandler) || 3472 handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler)) { 3473 // It is our signal handler. 3474 // Check for flags, reset system-used one! 3475 if ((int)sa.sa_flags != os::Aix::get_our_sigflags(sig)) { 3476 st->print(", flags was changed from " PTR32_FORMAT ", consider using jsig library", 3477 os::Aix::get_our_sigflags(sig)); 3478 } 3479 } 3480 st->cr(); 3481 } 3482 3483 #define DO_SIGNAL_CHECK(sig) \ 3484 if (!sigismember(&check_signal_done, sig)) \ 3485 os::Aix::check_signal_handler(sig) 3486 3487 // This method is a periodic task to check for misbehaving JNI applications 3488 // under CheckJNI, we can add any periodic checks here 3489 3490 void os::run_periodic_checks() { 3491 3492 if (check_signals == false) return; 3493 3494 // SEGV and BUS if overridden could potentially prevent 3495 // generation of hs*.log in the event of a crash, debugging 3496 // such a case can be very challenging, so we absolutely 3497 // check the following for a good measure: 3498 DO_SIGNAL_CHECK(SIGSEGV); 3499 DO_SIGNAL_CHECK(SIGILL); 3500 DO_SIGNAL_CHECK(SIGFPE); 3501 DO_SIGNAL_CHECK(SIGBUS); 3502 DO_SIGNAL_CHECK(SIGPIPE); 3503 DO_SIGNAL_CHECK(SIGXFSZ); 3504 if (UseSIGTRAP) { 3505 DO_SIGNAL_CHECK(SIGTRAP); 3506 } 3507 DO_SIGNAL_CHECK(SIGDANGER); 3508 3509 // ReduceSignalUsage allows the user to override these handlers 3510 // see comments at the very top and jvm_solaris.h 3511 if (!ReduceSignalUsage) { 3512 DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL); 3513 DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL); 3514 DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL); 3515 DO_SIGNAL_CHECK(BREAK_SIGNAL); 3516 } 3517 3518 DO_SIGNAL_CHECK(SR_signum); 3519 DO_SIGNAL_CHECK(INTERRUPT_SIGNAL); 3520 } 3521 3522 typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *); 3523 3524 static os_sigaction_t os_sigaction = NULL; 3525 3526 void os::Aix::check_signal_handler(int sig) { 3527 char buf[O_BUFLEN]; 3528 address jvmHandler = NULL; 3529 3530 struct sigaction act; 3531 if (os_sigaction == NULL) { 3532 // only trust the default sigaction, in case it has been interposed 3533 os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction"); 3534 if (os_sigaction == NULL) return; 3535 } 3536 3537 os_sigaction(sig, (struct sigaction*)NULL, &act); 3538 3539 address thisHandler = (act.sa_flags & SA_SIGINFO) 3540 ? CAST_FROM_FN_PTR(address, act.sa_sigaction) 3541 : CAST_FROM_FN_PTR(address, act.sa_handler); 3542 3543 switch(sig) { 3544 case SIGSEGV: 3545 case SIGBUS: 3546 case SIGFPE: 3547 case SIGPIPE: 3548 case SIGILL: 3549 case SIGXFSZ: 3550 // Renamed 'signalHandler' to avoid collision with other shared libs. 3551 jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)javaSignalHandler); 3552 break; 3553 3554 case SHUTDOWN1_SIGNAL: 3555 case SHUTDOWN2_SIGNAL: 3556 case SHUTDOWN3_SIGNAL: 3557 case BREAK_SIGNAL: 3558 jvmHandler = (address)user_handler(); 3559 break; 3560 3561 case INTERRUPT_SIGNAL: 3562 jvmHandler = CAST_FROM_FN_PTR(address, SIG_DFL); 3563 break; 3564 3565 default: 3566 if (sig == SR_signum) { 3567 jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler); 3568 } else { 3569 return; 3570 } 3571 break; 3572 } 3573 3574 if (thisHandler != jvmHandler) { 3575 tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN)); 3576 tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN)); 3577 tty->print_cr(" found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN)); 3578 // No need to check this sig any longer 3579 sigaddset(&check_signal_done, sig); 3580 // Running under non-interactive shell, SHUTDOWN2_SIGNAL will be reassigned SIG_IGN 3581 if (sig == SHUTDOWN2_SIGNAL && !isatty(fileno(stdin))) { 3582 tty->print_cr("Running in non-interactive shell, %s handler is replaced by shell", 3583 exception_name(sig, buf, O_BUFLEN)); 3584 } 3585 } else if (os::Aix::get_our_sigflags(sig) != 0 && (int)act.sa_flags != os::Aix::get_our_sigflags(sig)) { 3586 tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN)); 3587 tty->print("expected:" PTR32_FORMAT, os::Aix::get_our_sigflags(sig)); 3588 tty->print_cr(" found:" PTR32_FORMAT, act.sa_flags); 3589 // No need to check this sig any longer 3590 sigaddset(&check_signal_done, sig); 3591 } 3592 3593 // Dump all the signal 3594 if (sigismember(&check_signal_done, sig)) { 3595 print_signal_handlers(tty, buf, O_BUFLEN); 3596 } 3597 } 3598 3599 extern bool signal_name(int signo, char* buf, size_t len); 3600 3601 const char* os::exception_name(int exception_code, char* buf, size_t size) { 3602 if (0 < exception_code && exception_code <= SIGRTMAX) { 3603 // signal 3604 if (!signal_name(exception_code, buf, size)) { 3605 jio_snprintf(buf, size, "SIG%d", exception_code); 3606 } 3607 return buf; 3608 } else { 3609 return NULL; 3610 } 3611 } 3612 3613 // To install functions for atexit system call 3614 extern "C" { 3615 static void perfMemory_exit_helper() { 3616 perfMemory_exit(); 3617 } 3618 } 3619 3620 // This is called _before_ the most of global arguments have been parsed. 3621 void os::init(void) { 3622 // This is basic, we want to know if that ever changes. 3623 // (shared memory boundary is supposed to be a 256M aligned) 3624 assert(SHMLBA == ((uint64_t)0x10000000ULL)/*256M*/, "unexpected"); 3625 3626 // First off, we need to know whether we run on AIX or PASE, and 3627 // the OS level we run on. 3628 os::Aix::initialize_os_info(); 3629 3630 // Scan environment (SPEC1170 behaviour, etc) 3631 os::Aix::scan_environment(); 3632 3633 // Check which pages are supported by AIX. 3634 os::Aix::query_multipage_support(); 3635 3636 // Next, we need to initialize libo4 and libperfstat libraries. 3637 if (os::Aix::on_pase()) { 3638 os::Aix::initialize_libo4(); 3639 } else { 3640 os::Aix::initialize_libperfstat(); 3641 } 3642 3643 // Reset the perfstat information provided by ODM. 3644 if (os::Aix::on_aix()) { 3645 libperfstat::perfstat_reset(); 3646 } 3647 3648 // Now initialze basic system properties. Note that for some of the values we 3649 // need libperfstat etc. 3650 os::Aix::initialize_system_info(); 3651 3652 // Initialize large page support. 3653 if (UseLargePages) { 3654 os::large_page_init(); 3655 if (!UseLargePages) { 3656 // initialize os::_page_sizes 3657 _page_sizes[0] = Aix::page_size(); 3658 _page_sizes[1] = 0; 3659 if (Verbose) { 3660 fprintf(stderr, "Large Page initialization failed: setting UseLargePages=0.\n"); 3661 } 3662 } 3663 } else { 3664 // initialize os::_page_sizes 3665 _page_sizes[0] = Aix::page_size(); 3666 _page_sizes[1] = 0; 3667 } 3668 3669 // debug trace 3670 if (Verbose) { 3671 fprintf(stderr, "os::vm_page_size 0x%llX\n", os::vm_page_size()); 3672 fprintf(stderr, "os::large_page_size 0x%llX\n", os::large_page_size()); 3673 fprintf(stderr, "os::_page_sizes = ( "); 3674 for (int i = 0; _page_sizes[i]; i ++) { 3675 fprintf(stderr, " %s ", describe_pagesize(_page_sizes[i])); 3676 } 3677 fprintf(stderr, ")\n"); 3678 } 3679 3680 _initial_pid = getpid(); 3681 3682 clock_tics_per_sec = sysconf(_SC_CLK_TCK); 3683 3684 init_random(1234567); 3685 3686 ThreadCritical::initialize(); 3687 3688 // Main_thread points to the aboriginal thread. 3689 Aix::_main_thread = pthread_self(); 3690 3691 initial_time_count = os::elapsed_counter(); 3692 pthread_mutex_init(&dl_mutex, NULL); 3693 } 3694 3695 // This is called _after_ the global arguments have been parsed. 3696 jint os::init_2(void) { 3697 3698 trcVerbose("processor count: %d", os::_processor_count); 3699 trcVerbose("physical memory: %lu", Aix::_physical_memory); 3700 3701 // Initially build up the loaded dll map. 3702 LoadedLibraries::reload(); 3703 3704 const int page_size = Aix::page_size(); 3705 const int map_size = page_size; 3706 3707 address map_address = (address) MAP_FAILED; 3708 const int prot = PROT_READ; 3709 const int flags = MAP_PRIVATE|MAP_ANONYMOUS; 3710 3711 // use optimized addresses for the polling page, 3712 // e.g. map it to a special 32-bit address. 3713 if (OptimizePollingPageLocation) { 3714 // architecture-specific list of address wishes: 3715 address address_wishes[] = { 3716 // AIX: addresses lower than 0x30000000 don't seem to work on AIX. 3717 // PPC64: all address wishes are non-negative 32 bit values where 3718 // the lower 16 bits are all zero. we can load these addresses 3719 // with a single ppc_lis instruction. 3720 (address) 0x30000000, (address) 0x31000000, 3721 (address) 0x32000000, (address) 0x33000000, 3722 (address) 0x40000000, (address) 0x41000000, 3723 (address) 0x42000000, (address) 0x43000000, 3724 (address) 0x50000000, (address) 0x51000000, 3725 (address) 0x52000000, (address) 0x53000000, 3726 (address) 0x60000000, (address) 0x61000000, 3727 (address) 0x62000000, (address) 0x63000000 3728 }; 3729 int address_wishes_length = sizeof(address_wishes)/sizeof(address); 3730 3731 // iterate over the list of address wishes: 3732 for (int i=0; i<address_wishes_length; i++) { 3733 // try to map with current address wish. 3734 // AIX: AIX needs MAP_FIXED if we provide an address and mmap will 3735 // fail if the address is already mapped. 3736 map_address = (address) ::mmap(address_wishes[i] - (ssize_t)page_size, 3737 map_size, prot, 3738 flags | MAP_FIXED, 3739 -1, 0); 3740 if (Verbose) { 3741 fprintf(stderr, "SafePoint Polling Page address: %p (wish) => %p\n", 3742 address_wishes[i], map_address + (ssize_t)page_size); 3743 } 3744 3745 if (map_address + (ssize_t)page_size == address_wishes[i]) { 3746 // map succeeded and map_address is at wished address, exit loop. 3747 break; 3748 } 3749 3750 if (map_address != (address) MAP_FAILED) { 3751 // Map succeeded, but polling_page is not at wished address, unmap and continue. 3752 ::munmap(map_address, map_size); 3753 map_address = (address) MAP_FAILED; 3754 } 3755 // map failed, continue loop. 3756 } 3757 } // end OptimizePollingPageLocation 3758 3759 if (map_address == (address) MAP_FAILED) { 3760 map_address = (address) ::mmap(NULL, map_size, prot, flags, -1, 0); 3761 } 3762 guarantee(map_address != MAP_FAILED, "os::init_2: failed to allocate polling page"); 3763 os::set_polling_page(map_address); 3764 3765 if (!UseMembar) { 3766 address mem_serialize_page = (address) ::mmap(NULL, Aix::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 3767 guarantee(mem_serialize_page != NULL, "mmap Failed for memory serialize page"); 3768 os::set_memory_serialize_page(mem_serialize_page); 3769 3770 #ifndef PRODUCT 3771 if (Verbose && PrintMiscellaneous) 3772 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page); 3773 #endif 3774 } 3775 3776 // initialize suspend/resume support - must do this before signal_sets_init() 3777 if (SR_initialize() != 0) { 3778 perror("SR_initialize failed"); 3779 return JNI_ERR; 3780 } 3781 3782 Aix::signal_sets_init(); 3783 Aix::install_signal_handlers(); 3784 3785 // Check minimum allowable stack size for thread creation and to initialize 3786 // the java system classes, including StackOverflowError - depends on page 3787 // size. Add a page for compiler2 recursion in main thread. 3788 // Add in 2*BytesPerWord times page size to account for VM stack during 3789 // class initialization depending on 32 or 64 bit VM. 3790 os::Aix::min_stack_allowed = MAX2(os::Aix::min_stack_allowed, 3791 (size_t)(StackYellowPages+StackRedPages+StackShadowPages + 3792 2*BytesPerWord COMPILER2_PRESENT(+1)) * Aix::page_size()); 3793 3794 size_t threadStackSizeInBytes = ThreadStackSize * K; 3795 if (threadStackSizeInBytes != 0 && 3796 threadStackSizeInBytes < os::Aix::min_stack_allowed) { 3797 tty->print_cr("\nThe stack size specified is too small, " 3798 "Specify at least %dk", 3799 os::Aix::min_stack_allowed / K); 3800 return JNI_ERR; 3801 } 3802 3803 // Make the stack size a multiple of the page size so that 3804 // the yellow/red zones can be guarded. 3805 // Note that this can be 0, if no default stacksize was set. 3806 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes, vm_page_size())); 3807 3808 Aix::libpthread_init(); 3809 3810 if (MaxFDLimit) { 3811 // set the number of file descriptors to max. print out error 3812 // if getrlimit/setrlimit fails but continue regardless. 3813 struct rlimit nbr_files; 3814 int status = getrlimit(RLIMIT_NOFILE, &nbr_files); 3815 if (status != 0) { 3816 if (PrintMiscellaneous && (Verbose || WizardMode)) 3817 perror("os::init_2 getrlimit failed"); 3818 } else { 3819 nbr_files.rlim_cur = nbr_files.rlim_max; 3820 status = setrlimit(RLIMIT_NOFILE, &nbr_files); 3821 if (status != 0) { 3822 if (PrintMiscellaneous && (Verbose || WizardMode)) 3823 perror("os::init_2 setrlimit failed"); 3824 } 3825 } 3826 } 3827 3828 if (PerfAllowAtExitRegistration) { 3829 // only register atexit functions if PerfAllowAtExitRegistration is set. 3830 // atexit functions can be delayed until process exit time, which 3831 // can be problematic for embedded VM situations. Embedded VMs should 3832 // call DestroyJavaVM() to assure that VM resources are released. 3833 3834 // note: perfMemory_exit_helper atexit function may be removed in 3835 // the future if the appropriate cleanup code can be added to the 3836 // VM_Exit VMOperation's doit method. 3837 if (atexit(perfMemory_exit_helper) != 0) { 3838 warning("os::init_2 atexit(perfMemory_exit_helper) failed"); 3839 } 3840 } 3841 3842 return JNI_OK; 3843 } 3844 3845 // Mark the polling page as unreadable 3846 void os::make_polling_page_unreadable(void) { 3847 if (!guard_memory((char*)_polling_page, Aix::page_size())) { 3848 fatal("Could not disable polling page"); 3849 } 3850 }; 3851 3852 // Mark the polling page as readable 3853 void os::make_polling_page_readable(void) { 3854 // Changed according to os_linux.cpp. 3855 if (!checked_mprotect((char *)_polling_page, Aix::page_size(), PROT_READ)) { 3856 fatal(err_msg("Could not enable polling page at " PTR_FORMAT, _polling_page)); 3857 } 3858 }; 3859 3860 int os::active_processor_count() { 3861 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); 3862 assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check"); 3863 return online_cpus; 3864 } 3865 3866 void os::set_native_thread_name(const char *name) { 3867 // Not yet implemented. 3868 return; 3869 } 3870 3871 bool os::distribute_processes(uint length, uint* distribution) { 3872 // Not yet implemented. 3873 return false; 3874 } 3875 3876 bool os::bind_to_processor(uint processor_id) { 3877 // Not yet implemented. 3878 return false; 3879 } 3880 3881 void os::SuspendedThreadTask::internal_do_task() { 3882 if (do_suspend(_thread->osthread())) { 3883 SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext()); 3884 do_task(context); 3885 do_resume(_thread->osthread()); 3886 } 3887 } 3888 3889 class PcFetcher : public os::SuspendedThreadTask { 3890 public: 3891 PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {} 3892 ExtendedPC result(); 3893 protected: 3894 void do_task(const os::SuspendedThreadTaskContext& context); 3895 private: 3896 ExtendedPC _epc; 3897 }; 3898 3899 ExtendedPC PcFetcher::result() { 3900 guarantee(is_done(), "task is not done yet."); 3901 return _epc; 3902 } 3903 3904 void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) { 3905 Thread* thread = context.thread(); 3906 OSThread* osthread = thread->osthread(); 3907 if (osthread->ucontext() != NULL) { 3908 _epc = os::Aix::ucontext_get_pc((ucontext_t *) context.ucontext()); 3909 } else { 3910 // NULL context is unexpected, double-check this is the VMThread. 3911 guarantee(thread->is_VM_thread(), "can only be called for VMThread"); 3912 } 3913 } 3914 3915 // Suspends the target using the signal mechanism and then grabs the PC before 3916 // resuming the target. Used by the flat-profiler only 3917 ExtendedPC os::get_thread_pc(Thread* thread) { 3918 // Make sure that it is called by the watcher for the VMThread. 3919 assert(Thread::current()->is_Watcher_thread(), "Must be watcher"); 3920 assert(thread->is_VM_thread(), "Can only be called for VMThread"); 3921 3922 PcFetcher fetcher(thread); 3923 fetcher.run(); 3924 return fetcher.result(); 3925 } 3926 3927 // Not neede on Aix. 3928 // int os::Aix::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime) { 3929 // } 3930 3931 //////////////////////////////////////////////////////////////////////////////// 3932 // debug support 3933 3934 static address same_page(address x, address y) { 3935 intptr_t page_bits = -os::vm_page_size(); 3936 if ((intptr_t(x) & page_bits) == (intptr_t(y) & page_bits)) 3937 return x; 3938 else if (x > y) 3939 return (address)(intptr_t(y) | ~page_bits) + 1; 3940 else 3941 return (address)(intptr_t(y) & page_bits); 3942 } 3943 3944 bool os::find(address addr, outputStream* st) { 3945 3946 st->print(PTR_FORMAT ": ", addr); 3947 3948 const LoadedLibraryModule* lib = LoadedLibraries::find_for_text_address(addr); 3949 if (lib) { 3950 lib->print(st); 3951 return true; 3952 } else { 3953 lib = LoadedLibraries::find_for_data_address(addr); 3954 if (lib) { 3955 lib->print(st); 3956 return true; 3957 } else { 3958 st->print_cr("(outside any module)"); 3959 } 3960 } 3961 3962 return false; 3963 } 3964 3965 //////////////////////////////////////////////////////////////////////////////// 3966 // misc 3967 3968 // This does not do anything on Aix. This is basically a hook for being 3969 // able to use structured exception handling (thread-local exception filters) 3970 // on, e.g., Win32. 3971 void 3972 os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method, 3973 JavaCallArguments* args, Thread* thread) { 3974 f(value, method, args, thread); 3975 } 3976 3977 void os::print_statistics() { 3978 } 3979 3980 int os::message_box(const char* title, const char* message) { 3981 int i; 3982 fdStream err(defaultStream::error_fd()); 3983 for (i = 0; i < 78; i++) err.print_raw("="); 3984 err.cr(); 3985 err.print_raw_cr(title); 3986 for (i = 0; i < 78; i++) err.print_raw("-"); 3987 err.cr(); 3988 err.print_raw_cr(message); 3989 for (i = 0; i < 78; i++) err.print_raw("="); 3990 err.cr(); 3991 3992 char buf[16]; 3993 // Prevent process from exiting upon "read error" without consuming all CPU 3994 while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); } 3995 3996 return buf[0] == 'y' || buf[0] == 'Y'; 3997 } 3998 3999 int os::stat(const char *path, struct stat *sbuf) { 4000 char pathbuf[MAX_PATH]; 4001 if (strlen(path) > MAX_PATH - 1) { 4002 errno = ENAMETOOLONG; 4003 return -1; 4004 } 4005 os::native_path(strcpy(pathbuf, path)); 4006 return ::stat(pathbuf, sbuf); 4007 } 4008 4009 bool os::check_heap(bool force) { 4010 return true; 4011 } 4012 4013 // Is a (classpath) directory empty? 4014 bool os::dir_is_empty(const char* path) { 4015 DIR *dir = NULL; 4016 struct dirent *ptr; 4017 4018 dir = opendir(path); 4019 if (dir == NULL) return true; 4020 4021 /* Scan the directory */ 4022 bool result = true; 4023 char buf[sizeof(struct dirent) + MAX_PATH]; 4024 while (result && (ptr = ::readdir(dir)) != NULL) { 4025 if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { 4026 result = false; 4027 } 4028 } 4029 closedir(dir); 4030 return result; 4031 } 4032 4033 // This code originates from JDK's sysOpen and open64_w 4034 // from src/solaris/hpi/src/system_md.c 4035 4036 int os::open(const char *path, int oflag, int mode) { 4037 4038 if (strlen(path) > MAX_PATH - 1) { 4039 errno = ENAMETOOLONG; 4040 return -1; 4041 } 4042 int fd; 4043 4044 fd = ::open64(path, oflag, mode); 4045 if (fd == -1) return -1; 4046 4047 // If the open succeeded, the file might still be a directory. 4048 { 4049 struct stat64 buf64; 4050 int ret = ::fstat64(fd, &buf64); 4051 int st_mode = buf64.st_mode; 4052 4053 if (ret != -1) { 4054 if ((st_mode & S_IFMT) == S_IFDIR) { 4055 errno = EISDIR; 4056 ::close(fd); 4057 return -1; 4058 } 4059 } else { 4060 ::close(fd); 4061 return -1; 4062 } 4063 } 4064 4065 // All file descriptors that are opened in the JVM and not 4066 // specifically destined for a subprocess should have the 4067 // close-on-exec flag set. If we don't set it, then careless 3rd 4068 // party native code might fork and exec without closing all 4069 // appropriate file descriptors (e.g. as we do in closeDescriptors in 4070 // UNIXProcess.c), and this in turn might: 4071 // 4072 // - cause end-of-file to fail to be detected on some file 4073 // descriptors, resulting in mysterious hangs, or 4074 // 4075 // - might cause an fopen in the subprocess to fail on a system 4076 // suffering from bug 1085341. 4077 // 4078 // (Yes, the default setting of the close-on-exec flag is a Unix 4079 // design flaw.) 4080 // 4081 // See: 4082 // 1085341: 32-bit stdio routines should support file descriptors >255 4083 // 4843136: (process) pipe file descriptor from Runtime.exec not being closed 4084 // 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9 4085 #ifdef FD_CLOEXEC 4086 { 4087 int flags = ::fcntl(fd, F_GETFD); 4088 if (flags != -1) 4089 ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC); 4090 } 4091 #endif 4092 4093 return fd; 4094 } 4095 4096 // create binary file, rewriting existing file if required 4097 int os::create_binary_file(const char* path, bool rewrite_existing) { 4098 int oflags = O_WRONLY | O_CREAT; 4099 if (!rewrite_existing) { 4100 oflags |= O_EXCL; 4101 } 4102 return ::open64(path, oflags, S_IREAD | S_IWRITE); 4103 } 4104 4105 // return current position of file pointer 4106 jlong os::current_file_offset(int fd) { 4107 return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR); 4108 } 4109 4110 // move file pointer to the specified offset 4111 jlong os::seek_to_file_offset(int fd, jlong offset) { 4112 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET); 4113 } 4114 4115 // This code originates from JDK's sysAvailable 4116 // from src/solaris/hpi/src/native_threads/src/sys_api_td.c 4117 4118 int os::available(int fd, jlong *bytes) { 4119 jlong cur, end; 4120 int mode; 4121 struct stat64 buf64; 4122 4123 if (::fstat64(fd, &buf64) >= 0) { 4124 mode = buf64.st_mode; 4125 if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) { 4126 // XXX: is the following call interruptible? If so, this might 4127 // need to go through the INTERRUPT_IO() wrapper as for other 4128 // blocking, interruptible calls in this file. 4129 int n; 4130 if (::ioctl(fd, FIONREAD, &n) >= 0) { 4131 *bytes = n; 4132 return 1; 4133 } 4134 } 4135 } 4136 if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) { 4137 return 0; 4138 } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) { 4139 return 0; 4140 } else if (::lseek64(fd, cur, SEEK_SET) == -1) { 4141 return 0; 4142 } 4143 *bytes = end - cur; 4144 return 1; 4145 } 4146 4147 // Map a block of memory. 4148 char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset, 4149 char *addr, size_t bytes, bool read_only, 4150 bool allow_exec) { 4151 int prot; 4152 int flags = MAP_PRIVATE; 4153 4154 if (read_only) { 4155 prot = PROT_READ; 4156 } else { 4157 prot = PROT_READ | PROT_WRITE; 4158 } 4159 4160 if (allow_exec) { 4161 prot |= PROT_EXEC; 4162 } 4163 4164 if (addr != NULL) { 4165 flags |= MAP_FIXED; 4166 } 4167 4168 char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags, 4169 fd, file_offset); 4170 if (mapped_address == MAP_FAILED) { 4171 return NULL; 4172 } 4173 return mapped_address; 4174 } 4175 4176 // Remap a block of memory. 4177 char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset, 4178 char *addr, size_t bytes, bool read_only, 4179 bool allow_exec) { 4180 // same as map_memory() on this OS 4181 return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only, 4182 allow_exec); 4183 } 4184 4185 // Unmap a block of memory. 4186 bool os::pd_unmap_memory(char* addr, size_t bytes) { 4187 return munmap(addr, bytes) == 0; 4188 } 4189 4190 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool) 4191 // are used by JVM M&M and JVMTI to get user+sys or user CPU time 4192 // of a thread. 4193 // 4194 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns 4195 // the fast estimate available on the platform. 4196 4197 jlong os::current_thread_cpu_time() { 4198 // return user + sys since the cost is the same 4199 const jlong n = os::thread_cpu_time(Thread::current(), true /* user + sys */); 4200 assert(n >= 0, "negative CPU time"); 4201 return n; 4202 } 4203 4204 jlong os::thread_cpu_time(Thread* thread) { 4205 // consistent with what current_thread_cpu_time() returns 4206 const jlong n = os::thread_cpu_time(thread, true /* user + sys */); 4207 assert(n >= 0, "negative CPU time"); 4208 return n; 4209 } 4210 4211 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) { 4212 const jlong n = os::thread_cpu_time(Thread::current(), user_sys_cpu_time); 4213 assert(n >= 0, "negative CPU time"); 4214 return n; 4215 } 4216 4217 static bool thread_cpu_time_unchecked(Thread* thread, jlong* p_sys_time, jlong* p_user_time) { 4218 bool error = false; 4219 4220 jlong sys_time = 0; 4221 jlong user_time = 0; 4222 4223 // Reimplemented using getthrds64(). 4224 // 4225 // Works like this: 4226 // For the thread in question, get the kernel thread id. Then get the 4227 // kernel thread statistics using that id. 4228 // 4229 // This only works of course when no pthread scheduling is used, 4230 // i.e. there is a 1:1 relationship to kernel threads. 4231 // On AIX, see AIXTHREAD_SCOPE variable. 4232 4233 pthread_t pthtid = thread->osthread()->pthread_id(); 4234 4235 // retrieve kernel thread id for the pthread: 4236 tid64_t tid = 0; 4237 struct __pthrdsinfo pinfo; 4238 // I just love those otherworldly IBM APIs which force me to hand down 4239 // dummy buffers for stuff I dont care for... 4240 char dummy[1]; 4241 int dummy_size = sizeof(dummy); 4242 if (pthread_getthrds_np(&pthtid, PTHRDSINFO_QUERY_TID, &pinfo, sizeof(pinfo), 4243 dummy, &dummy_size) == 0) { 4244 tid = pinfo.__pi_tid; 4245 } else { 4246 tty->print_cr("pthread_getthrds_np failed."); 4247 error = true; 4248 } 4249 4250 // retrieve kernel timing info for that kernel thread 4251 if (!error) { 4252 struct thrdentry64 thrdentry; 4253 if (getthrds64(getpid(), &thrdentry, sizeof(thrdentry), &tid, 1) == 1) { 4254 sys_time = thrdentry.ti_ru.ru_stime.tv_sec * 1000000000LL + thrdentry.ti_ru.ru_stime.tv_usec * 1000LL; 4255 user_time = thrdentry.ti_ru.ru_utime.tv_sec * 1000000000LL + thrdentry.ti_ru.ru_utime.tv_usec * 1000LL; 4256 } else { 4257 tty->print_cr("pthread_getthrds_np failed."); 4258 error = true; 4259 } 4260 } 4261 4262 if (p_sys_time) { 4263 *p_sys_time = sys_time; 4264 } 4265 4266 if (p_user_time) { 4267 *p_user_time = user_time; 4268 } 4269 4270 if (error) { 4271 return false; 4272 } 4273 4274 return true; 4275 } 4276 4277 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) { 4278 jlong sys_time; 4279 jlong user_time; 4280 4281 if (!thread_cpu_time_unchecked(thread, &sys_time, &user_time)) { 4282 return -1; 4283 } 4284 4285 return user_sys_cpu_time ? sys_time + user_time : user_time; 4286 } 4287 4288 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) { 4289 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits 4290 info_ptr->may_skip_backward = false; // elapsed time not wall time 4291 info_ptr->may_skip_forward = false; // elapsed time not wall time 4292 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned 4293 } 4294 4295 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) { 4296 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits 4297 info_ptr->may_skip_backward = false; // elapsed time not wall time 4298 info_ptr->may_skip_forward = false; // elapsed time not wall time 4299 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned 4300 } 4301 4302 bool os::is_thread_cpu_time_supported() { 4303 return true; 4304 } 4305 4306 // System loadavg support. Returns -1 if load average cannot be obtained. 4307 // For now just return the system wide load average (no processor sets). 4308 int os::loadavg(double values[], int nelem) { 4309 4310 // Implemented using libperfstat on AIX. 4311 4312 guarantee(nelem >= 0 && nelem <= 3, "argument error"); 4313 guarantee(values, "argument error"); 4314 4315 if (os::Aix::on_pase()) { 4316 Unimplemented(); 4317 return -1; 4318 } else { 4319 // AIX: use libperfstat 4320 // 4321 // See also: 4322 // http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/perfstat_cputot.htm 4323 // /usr/include/libperfstat.h: 4324 4325 // Use the already AIX version independent get_cpuinfo. 4326 os::Aix::cpuinfo_t ci; 4327 if (os::Aix::get_cpuinfo(&ci)) { 4328 for (int i = 0; i < nelem; i++) { 4329 values[i] = ci.loadavg[i]; 4330 } 4331 } else { 4332 return -1; 4333 } 4334 return nelem; 4335 } 4336 } 4337 4338 void os::pause() { 4339 char filename[MAX_PATH]; 4340 if (PauseAtStartupFile && PauseAtStartupFile[0]) { 4341 jio_snprintf(filename, MAX_PATH, PauseAtStartupFile); 4342 } else { 4343 jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id()); 4344 } 4345 4346 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 4347 if (fd != -1) { 4348 struct stat buf; 4349 ::close(fd); 4350 while (::stat(filename, &buf) == 0) { 4351 (void)::poll(NULL, 0, 100); 4352 } 4353 } else { 4354 jio_fprintf(stderr, 4355 "Could not open pause file '%s', continuing immediately.\n", filename); 4356 } 4357 } 4358 4359 bool os::Aix::is_primordial_thread() { 4360 if (pthread_self() == (pthread_t)1) { 4361 return true; 4362 } else { 4363 return false; 4364 } 4365 } 4366 4367 // OS recognitions (PASE/AIX, OS level) call this before calling any 4368 // one of Aix::on_pase(), Aix::os_version() static 4369 void os::Aix::initialize_os_info() { 4370 4371 assert(_on_pase == -1 && _os_version == -1, "already called."); 4372 4373 struct utsname uts; 4374 memset(&uts, 0, sizeof(uts)); 4375 strcpy(uts.sysname, "?"); 4376 if (::uname(&uts) == -1) { 4377 trc("uname failed (%d)", errno); 4378 guarantee(0, "Could not determine whether we run on AIX or PASE"); 4379 } else { 4380 trcVerbose("uname says: sysname \"%s\" version \"%s\" release \"%s\" " 4381 "node \"%s\" machine \"%s\"\n", 4382 uts.sysname, uts.version, uts.release, uts.nodename, uts.machine); 4383 const int major = atoi(uts.version); 4384 assert(major > 0, "invalid OS version"); 4385 const int minor = atoi(uts.release); 4386 assert(minor > 0, "invalid OS release"); 4387 _os_version = (major << 8) | minor; 4388 if (strcmp(uts.sysname, "OS400") == 0) { 4389 Unimplemented(); 4390 } else if (strcmp(uts.sysname, "AIX") == 0) { 4391 // We run on AIX. We do not support versions older than AIX 5.3. 4392 _on_pase = 0; 4393 if (_os_version < 0x0503) { 4394 trc("AIX release older than AIX 5.3 not supported."); 4395 assert(false, "AIX release too old."); 4396 } else { 4397 trcVerbose("We run on AIX %d.%d\n", major, minor); 4398 } 4399 } else { 4400 assert(false, "unknown OS"); 4401 } 4402 } 4403 4404 guarantee(_on_pase != -1 && _os_version, "Could not determine AIX/OS400 release"); 4405 } // end: os::Aix::initialize_os_info() 4406 4407 // Scan environment for important settings which might effect the VM. 4408 // Trace out settings. Warn about invalid settings and/or correct them. 4409 // 4410 // Must run after os::Aix::initialue_os_info(). 4411 void os::Aix::scan_environment() { 4412 4413 char* p; 4414 int rc; 4415 4416 // Warn explicity if EXTSHM=ON is used. That switch changes how 4417 // System V shared memory behaves. One effect is that page size of 4418 // shared memory cannot be change dynamically, effectivly preventing 4419 // large pages from working. 4420 // This switch was needed on AIX 32bit, but on AIX 64bit the general 4421 // recommendation is (in OSS notes) to switch it off. 4422 p = ::getenv("EXTSHM"); 4423 if (Verbose) { 4424 fprintf(stderr, "EXTSHM=%s.\n", p ? p : "<unset>"); 4425 } 4426 if (p && strcmp(p, "ON") == 0) { 4427 fprintf(stderr, "Unsupported setting: EXTSHM=ON. Large Page support will be disabled.\n"); 4428 _extshm = 1; 4429 } else { 4430 _extshm = 0; 4431 } 4432 4433 // SPEC1170 behaviour: will change the behaviour of a number of POSIX APIs. 4434 // Not tested, not supported. 4435 // 4436 // Note that it might be worth the trouble to test and to require it, if only to 4437 // get useful return codes for mprotect. 4438 // 4439 // Note: Setting XPG_SUS_ENV in the process is too late. Must be set earlier (before 4440 // exec() ? before loading the libjvm ? ....) 4441 p = ::getenv("XPG_SUS_ENV"); 4442 trcVerbose("XPG_SUS_ENV=%s.", p ? p : "<unset>"); 4443 if (p && strcmp(p, "ON") == 0) { 4444 _xpg_sus_mode = 1; 4445 trc("Unsupported setting: XPG_SUS_ENV=ON"); 4446 // This is not supported. Worst of all, it changes behaviour of mmap MAP_FIXED to 4447 // clobber address ranges. If we ever want to support that, we have to do some 4448 // testing first. 4449 guarantee(false, "XPG_SUS_ENV=ON not supported"); 4450 } else { 4451 _xpg_sus_mode = 0; 4452 } 4453 4454 // Switch off AIX internal (pthread) guard pages. This has 4455 // immediate effect for any pthread_create calls which follow. 4456 p = ::getenv("AIXTHREAD_GUARDPAGES"); 4457 trcVerbose("AIXTHREAD_GUARDPAGES=%s.", p ? p : "<unset>"); 4458 rc = ::putenv("AIXTHREAD_GUARDPAGES=0"); 4459 guarantee(rc == 0, ""); 4460 4461 } // end: os::Aix::scan_environment() 4462 4463 // PASE: initialize the libo4 library (AS400 PASE porting library). 4464 void os::Aix::initialize_libo4() { 4465 Unimplemented(); 4466 } 4467 4468 // AIX: initialize the libperfstat library (we load this dynamically 4469 // because it is only available on AIX. 4470 void os::Aix::initialize_libperfstat() { 4471 4472 assert(os::Aix::on_aix(), "AIX only"); 4473 4474 if (!libperfstat::init()) { 4475 trc("libperfstat initialization failed."); 4476 assert(false, "libperfstat initialization failed"); 4477 } else { 4478 if (Verbose) { 4479 fprintf(stderr, "libperfstat initialized.\n"); 4480 } 4481 } 4482 } // end: os::Aix::initialize_libperfstat 4483 4484 ///////////////////////////////////////////////////////////////////////////// 4485 // thread stack 4486 4487 // function to query the current stack size using pthread_getthrds_np 4488 // 4489 // ! do not change anything here unless you know what you are doing ! 4490 static void query_stack_dimensions(address* p_stack_base, size_t* p_stack_size) { 4491 4492 // This only works when invoked on a pthread. As we agreed not to use 4493 // primordial threads anyway, I assert here 4494 guarantee(!os::Aix::is_primordial_thread(), "not allowed on the primordial thread"); 4495 4496 // information about this api can be found (a) in the pthread.h header and 4497 // (b) in http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_getthrds_np.htm 4498 // 4499 // The use of this API to find out the current stack is kind of undefined. 4500 // But after a lot of tries and asking IBM about it, I concluded that it is safe 4501 // enough for cases where I let the pthread library create its stacks. For cases 4502 // where I create an own stack and pass this to pthread_create, it seems not to 4503 // work (the returned stack size in that case is 0). 4504 4505 pthread_t tid = pthread_self(); 4506 struct __pthrdsinfo pinfo; 4507 char dummy[1]; // we only need this to satisfy the api and to not get E 4508 int dummy_size = sizeof(dummy); 4509 4510 memset(&pinfo, 0, sizeof(pinfo)); 4511 4512 const int rc = pthread_getthrds_np (&tid, PTHRDSINFO_QUERY_ALL, &pinfo, 4513 sizeof(pinfo), dummy, &dummy_size); 4514 4515 if (rc != 0) { 4516 fprintf(stderr, "pthread_getthrds_np failed (%d)\n", rc); 4517 guarantee(0, "pthread_getthrds_np failed"); 4518 } 4519 4520 guarantee(pinfo.__pi_stackend, "returned stack base invalid"); 4521 4522 // the following can happen when invoking pthread_getthrds_np on a pthread running on a user provided stack 4523 // (when handing down a stack to pthread create, see pthread_attr_setstackaddr). 4524 // Not sure what to do here - I feel inclined to forbid this use case completely. 4525 guarantee(pinfo.__pi_stacksize, "returned stack size invalid"); 4526 4527 // On AIX, stacks are not necessarily page aligned so round the base and size accordingly 4528 if (p_stack_base) { 4529 (*p_stack_base) = (address) align_size_up((intptr_t)pinfo.__pi_stackend, os::Aix::stack_page_size()); 4530 } 4531 4532 if (p_stack_size) { 4533 (*p_stack_size) = pinfo.__pi_stacksize - os::Aix::stack_page_size(); 4534 } 4535 4536 #ifndef PRODUCT 4537 if (Verbose) { 4538 fprintf(stderr, 4539 "query_stack_dimensions() -> real stack_base=" INTPTR_FORMAT ", real stack_addr=" INTPTR_FORMAT 4540 ", real stack_size=" INTPTR_FORMAT 4541 ", stack_base=" INTPTR_FORMAT ", stack_size=" INTPTR_FORMAT "\n", 4542 (intptr_t)pinfo.__pi_stackend, (intptr_t)pinfo.__pi_stackaddr, pinfo.__pi_stacksize, 4543 (intptr_t)align_size_up((intptr_t)pinfo.__pi_stackend, os::Aix::stack_page_size()), 4544 pinfo.__pi_stacksize - os::Aix::stack_page_size()); 4545 } 4546 #endif 4547 4548 } // end query_stack_dimensions 4549 4550 // get the current stack base from the OS (actually, the pthread library) 4551 address os::current_stack_base() { 4552 address p; 4553 query_stack_dimensions(&p, 0); 4554 return p; 4555 } 4556 4557 // get the current stack size from the OS (actually, the pthread library) 4558 size_t os::current_stack_size() { 4559 size_t s; 4560 query_stack_dimensions(0, &s); 4561 return s; 4562 } 4563 4564 // Refer to the comments in os_solaris.cpp park-unpark. 4565 // 4566 // Beware -- Some versions of NPTL embody a flaw where pthread_cond_timedwait() can 4567 // hang indefinitely. For instance NPTL 0.60 on 2.4.21-4ELsmp is vulnerable. 4568 // For specifics regarding the bug see GLIBC BUGID 261237 : 4569 // http://www.mail-archive.com/debian-glibc@lists.debian.org/msg10837.html. 4570 // Briefly, pthread_cond_timedwait() calls with an expiry time that's not in the future 4571 // will either hang or corrupt the condvar, resulting in subsequent hangs if the condvar 4572 // is used. (The simple C test-case provided in the GLIBC bug report manifests the 4573 // hang). The JVM is vulernable via sleep(), Object.wait(timo), LockSupport.parkNanos() 4574 // and monitorenter when we're using 1-0 locking. All those operations may result in 4575 // calls to pthread_cond_timedwait(). Using LD_ASSUME_KERNEL to use an older version 4576 // of libpthread avoids the problem, but isn't practical. 4577 // 4578 // Possible remedies: 4579 // 4580 // 1. Establish a minimum relative wait time. 50 to 100 msecs seems to work. 4581 // This is palliative and probabilistic, however. If the thread is preempted 4582 // between the call to compute_abstime() and pthread_cond_timedwait(), more 4583 // than the minimum period may have passed, and the abstime may be stale (in the 4584 // past) resultin in a hang. Using this technique reduces the odds of a hang 4585 // but the JVM is still vulnerable, particularly on heavily loaded systems. 4586 // 4587 // 2. Modify park-unpark to use per-thread (per ParkEvent) pipe-pairs instead 4588 // of the usual flag-condvar-mutex idiom. The write side of the pipe is set 4589 // NDELAY. unpark() reduces to write(), park() reduces to read() and park(timo) 4590 // reduces to poll()+read(). This works well, but consumes 2 FDs per extant 4591 // thread. 4592 // 4593 // 3. Embargo pthread_cond_timedwait() and implement a native "chron" thread 4594 // that manages timeouts. We'd emulate pthread_cond_timedwait() by enqueuing 4595 // a timeout request to the chron thread and then blocking via pthread_cond_wait(). 4596 // This also works well. In fact it avoids kernel-level scalability impediments 4597 // on certain platforms that don't handle lots of active pthread_cond_timedwait() 4598 // timers in a graceful fashion. 4599 // 4600 // 4. When the abstime value is in the past it appears that control returns 4601 // correctly from pthread_cond_timedwait(), but the condvar is left corrupt. 4602 // Subsequent timedwait/wait calls may hang indefinitely. Given that, we 4603 // can avoid the problem by reinitializing the condvar -- by cond_destroy() 4604 // followed by cond_init() -- after all calls to pthread_cond_timedwait(). 4605 // It may be possible to avoid reinitialization by checking the return 4606 // value from pthread_cond_timedwait(). In addition to reinitializing the 4607 // condvar we must establish the invariant that cond_signal() is only called 4608 // within critical sections protected by the adjunct mutex. This prevents 4609 // cond_signal() from "seeing" a condvar that's in the midst of being 4610 // reinitialized or that is corrupt. Sadly, this invariant obviates the 4611 // desirable signal-after-unlock optimization that avoids futile context switching. 4612 // 4613 // I'm also concerned that some versions of NTPL might allocate an auxilliary 4614 // structure when a condvar is used or initialized. cond_destroy() would 4615 // release the helper structure. Our reinitialize-after-timedwait fix 4616 // put excessive stress on malloc/free and locks protecting the c-heap. 4617 // 4618 // We currently use (4). See the WorkAroundNTPLTimedWaitHang flag. 4619 // It may be possible to refine (4) by checking the kernel and NTPL verisons 4620 // and only enabling the work-around for vulnerable environments. 4621 4622 // utility to compute the abstime argument to timedwait: 4623 // millis is the relative timeout time 4624 // abstime will be the absolute timeout time 4625 // TODO: replace compute_abstime() with unpackTime() 4626 4627 static struct timespec* compute_abstime(timespec* abstime, jlong millis) { 4628 if (millis < 0) millis = 0; 4629 struct timeval now; 4630 int status = gettimeofday(&now, NULL); 4631 assert(status == 0, "gettimeofday"); 4632 jlong seconds = millis / 1000; 4633 millis %= 1000; 4634 if (seconds > 50000000) { // see man cond_timedwait(3T) 4635 seconds = 50000000; 4636 } 4637 abstime->tv_sec = now.tv_sec + seconds; 4638 long usec = now.tv_usec + millis * 1000; 4639 if (usec >= 1000000) { 4640 abstime->tv_sec += 1; 4641 usec -= 1000000; 4642 } 4643 abstime->tv_nsec = usec * 1000; 4644 return abstime; 4645 } 4646 4647 // Test-and-clear _Event, always leaves _Event set to 0, returns immediately. 4648 // Conceptually TryPark() should be equivalent to park(0). 4649 4650 int os::PlatformEvent::TryPark() { 4651 for (;;) { 4652 const int v = _Event; 4653 guarantee ((v == 0) || (v == 1), "invariant"); 4654 if (Atomic::cmpxchg (0, &_Event, v) == v) return v; 4655 } 4656 } 4657 4658 void os::PlatformEvent::park() { // AKA "down()" 4659 // Invariant: Only the thread associated with the Event/PlatformEvent 4660 // may call park(). 4661 // TODO: assert that _Assoc != NULL or _Assoc == Self 4662 int v; 4663 for (;;) { 4664 v = _Event; 4665 if (Atomic::cmpxchg (v-1, &_Event, v) == v) break; 4666 } 4667 guarantee (v >= 0, "invariant"); 4668 if (v == 0) { 4669 // Do this the hard way by blocking ... 4670 int status = pthread_mutex_lock(_mutex); 4671 assert_status(status == 0, status, "mutex_lock"); 4672 guarantee (_nParked == 0, "invariant"); 4673 ++ _nParked; 4674 while (_Event < 0) { 4675 status = pthread_cond_wait(_cond, _mutex); 4676 assert_status(status == 0 || status == ETIMEDOUT, status, "cond_timedwait"); 4677 } 4678 -- _nParked; 4679 4680 // In theory we could move the ST of 0 into _Event past the unlock(), 4681 // but then we'd need a MEMBAR after the ST. 4682 _Event = 0; 4683 status = pthread_mutex_unlock(_mutex); 4684 assert_status(status == 0, status, "mutex_unlock"); 4685 } 4686 guarantee (_Event >= 0, "invariant"); 4687 } 4688 4689 int os::PlatformEvent::park(jlong millis) { 4690 guarantee (_nParked == 0, "invariant"); 4691 4692 int v; 4693 for (;;) { 4694 v = _Event; 4695 if (Atomic::cmpxchg (v-1, &_Event, v) == v) break; 4696 } 4697 guarantee (v >= 0, "invariant"); 4698 if (v != 0) return OS_OK; 4699 4700 // We do this the hard way, by blocking the thread. 4701 // Consider enforcing a minimum timeout value. 4702 struct timespec abst; 4703 compute_abstime(&abst, millis); 4704 4705 int ret = OS_TIMEOUT; 4706 int status = pthread_mutex_lock(_mutex); 4707 assert_status(status == 0, status, "mutex_lock"); 4708 guarantee (_nParked == 0, "invariant"); 4709 ++_nParked; 4710 4711 // Object.wait(timo) will return because of 4712 // (a) notification 4713 // (b) timeout 4714 // (c) thread.interrupt 4715 // 4716 // Thread.interrupt and object.notify{All} both call Event::set. 4717 // That is, we treat thread.interrupt as a special case of notification. 4718 // We ignore spurious OS wakeups unless FilterSpuriousWakeups is false. 4719 // We assume all ETIME returns are valid. 4720 // 4721 // TODO: properly differentiate simultaneous notify+interrupt. 4722 // In that case, we should propagate the notify to another waiter. 4723 4724 while (_Event < 0) { 4725 status = pthread_cond_timedwait(_cond, _mutex, &abst); 4726 assert_status(status == 0 || status == ETIMEDOUT, 4727 status, "cond_timedwait"); 4728 if (!FilterSpuriousWakeups) break; // previous semantics 4729 if (status == ETIMEDOUT) break; 4730 // We consume and ignore EINTR and spurious wakeups. 4731 } 4732 --_nParked; 4733 if (_Event >= 0) { 4734 ret = OS_OK; 4735 } 4736 _Event = 0; 4737 status = pthread_mutex_unlock(_mutex); 4738 assert_status(status == 0, status, "mutex_unlock"); 4739 assert (_nParked == 0, "invariant"); 4740 return ret; 4741 } 4742 4743 void os::PlatformEvent::unpark() { 4744 int v, AnyWaiters; 4745 for (;;) { 4746 v = _Event; 4747 if (v > 0) { 4748 // The LD of _Event could have reordered or be satisfied 4749 // by a read-aside from this processor's write buffer. 4750 // To avoid problems execute a barrier and then 4751 // ratify the value. 4752 OrderAccess::fence(); 4753 if (_Event == v) return; 4754 continue; 4755 } 4756 if (Atomic::cmpxchg (v+1, &_Event, v) == v) break; 4757 } 4758 if (v < 0) { 4759 // Wait for the thread associated with the event to vacate 4760 int status = pthread_mutex_lock(_mutex); 4761 assert_status(status == 0, status, "mutex_lock"); 4762 AnyWaiters = _nParked; 4763 4764 if (AnyWaiters != 0) { 4765 // We intentional signal *after* dropping the lock 4766 // to avoid a common class of futile wakeups. 4767 status = pthread_cond_signal(_cond); 4768 assert_status(status == 0, status, "cond_signal"); 4769 } 4770 // Mutex should be locked for pthread_cond_signal(_cond). 4771 status = pthread_mutex_unlock(_mutex); 4772 assert_status(status == 0, status, "mutex_unlock"); 4773 } 4774 4775 // Note that we signal() _after dropping the lock for "immortal" Events. 4776 // This is safe and avoids a common class of futile wakeups. In rare 4777 // circumstances this can cause a thread to return prematurely from 4778 // cond_{timed}wait() but the spurious wakeup is benign and the victim will 4779 // simply re-test the condition and re-park itself. 4780 } 4781 4782 4783 // JSR166 4784 // ------------------------------------------------------- 4785 4786 // 4787 // The solaris and linux implementations of park/unpark are fairly 4788 // conservative for now, but can be improved. They currently use a 4789 // mutex/condvar pair, plus a a count. 4790 // Park decrements count if > 0, else does a condvar wait. Unpark 4791 // sets count to 1 and signals condvar. Only one thread ever waits 4792 // on the condvar. Contention seen when trying to park implies that someone 4793 // is unparking you, so don't wait. And spurious returns are fine, so there 4794 // is no need to track notifications. 4795 // 4796 4797 #define MAX_SECS 100000000 4798 // 4799 // This code is common to linux and solaris and will be moved to a 4800 // common place in dolphin. 4801 // 4802 // The passed in time value is either a relative time in nanoseconds 4803 // or an absolute time in milliseconds. Either way it has to be unpacked 4804 // into suitable seconds and nanoseconds components and stored in the 4805 // given timespec structure. 4806 // Given time is a 64-bit value and the time_t used in the timespec is only 4807 // a signed-32-bit value (except on 64-bit Linux) we have to watch for 4808 // overflow if times way in the future are given. Further on Solaris versions 4809 // prior to 10 there is a restriction (see cond_timedwait) that the specified 4810 // number of seconds, in abstime, is less than current_time + 100,000,000. 4811 // As it will be 28 years before "now + 100000000" will overflow we can 4812 // ignore overflow and just impose a hard-limit on seconds using the value 4813 // of "now + 100,000,000". This places a limit on the timeout of about 3.17 4814 // years from "now". 4815 // 4816 4817 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) { 4818 assert (time > 0, "convertTime"); 4819 4820 struct timeval now; 4821 int status = gettimeofday(&now, NULL); 4822 assert(status == 0, "gettimeofday"); 4823 4824 time_t max_secs = now.tv_sec + MAX_SECS; 4825 4826 if (isAbsolute) { 4827 jlong secs = time / 1000; 4828 if (secs > max_secs) { 4829 absTime->tv_sec = max_secs; 4830 } 4831 else { 4832 absTime->tv_sec = secs; 4833 } 4834 absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC; 4835 } 4836 else { 4837 jlong secs = time / NANOSECS_PER_SEC; 4838 if (secs >= MAX_SECS) { 4839 absTime->tv_sec = max_secs; 4840 absTime->tv_nsec = 0; 4841 } 4842 else { 4843 absTime->tv_sec = now.tv_sec + secs; 4844 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000; 4845 if (absTime->tv_nsec >= NANOSECS_PER_SEC) { 4846 absTime->tv_nsec -= NANOSECS_PER_SEC; 4847 ++absTime->tv_sec; // note: this must be <= max_secs 4848 } 4849 } 4850 } 4851 assert(absTime->tv_sec >= 0, "tv_sec < 0"); 4852 assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs"); 4853 assert(absTime->tv_nsec >= 0, "tv_nsec < 0"); 4854 assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec"); 4855 } 4856 4857 void Parker::park(bool isAbsolute, jlong time) { 4858 // Optional fast-path check: 4859 // Return immediately if a permit is available. 4860 if (_counter > 0) { 4861 _counter = 0; 4862 OrderAccess::fence(); 4863 return; 4864 } 4865 4866 Thread* thread = Thread::current(); 4867 assert(thread->is_Java_thread(), "Must be JavaThread"); 4868 JavaThread *jt = (JavaThread *)thread; 4869 4870 // Optional optimization -- avoid state transitions if there's an interrupt pending. 4871 // Check interrupt before trying to wait 4872 if (Thread::is_interrupted(thread, false)) { 4873 return; 4874 } 4875 4876 // Next, demultiplex/decode time arguments 4877 timespec absTime; 4878 if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all 4879 return; 4880 } 4881 if (time > 0) { 4882 unpackTime(&absTime, isAbsolute, time); 4883 } 4884 4885 // Enter safepoint region 4886 // Beware of deadlocks such as 6317397. 4887 // The per-thread Parker:: mutex is a classic leaf-lock. 4888 // In particular a thread must never block on the Threads_lock while 4889 // holding the Parker:: mutex. If safepoints are pending both the 4890 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 4891 ThreadBlockInVM tbivm(jt); 4892 4893 // Don't wait if cannot get lock since interference arises from 4894 // unblocking. Also. check interrupt before trying wait 4895 if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) { 4896 return; 4897 } 4898 4899 int status; 4900 if (_counter > 0) { // no wait needed 4901 _counter = 0; 4902 status = pthread_mutex_unlock(_mutex); 4903 assert (status == 0, "invariant"); 4904 OrderAccess::fence(); 4905 return; 4906 } 4907 4908 #ifdef ASSERT 4909 // Don't catch signals while blocked; let the running threads have the signals. 4910 // (This allows a debugger to break into the running thread.) 4911 sigset_t oldsigs; 4912 sigset_t* allowdebug_blocked = os::Aix::allowdebug_blocked_signals(); 4913 pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs); 4914 #endif 4915 4916 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 4917 jt->set_suspend_equivalent(); 4918 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 4919 4920 if (time == 0) { 4921 status = pthread_cond_wait (_cond, _mutex); 4922 } else { 4923 status = pthread_cond_timedwait (_cond, _mutex, &absTime); 4924 if (status != 0 && WorkAroundNPTLTimedWaitHang) { 4925 pthread_cond_destroy (_cond); 4926 pthread_cond_init (_cond, NULL); 4927 } 4928 } 4929 assert_status(status == 0 || status == EINTR || 4930 status == ETIME || status == ETIMEDOUT, 4931 status, "cond_timedwait"); 4932 4933 #ifdef ASSERT 4934 pthread_sigmask(SIG_SETMASK, &oldsigs, NULL); 4935 #endif 4936 4937 _counter = 0; 4938 status = pthread_mutex_unlock(_mutex); 4939 assert_status(status == 0, status, "invariant"); 4940 // If externally suspended while waiting, re-suspend 4941 if (jt->handle_special_suspend_equivalent_condition()) { 4942 jt->java_suspend_self(); 4943 } 4944 4945 OrderAccess::fence(); 4946 } 4947 4948 void Parker::unpark() { 4949 int s, status; 4950 status = pthread_mutex_lock(_mutex); 4951 assert (status == 0, "invariant"); 4952 s = _counter; 4953 _counter = 1; 4954 if (s < 1) { 4955 if (WorkAroundNPTLTimedWaitHang) { 4956 status = pthread_cond_signal (_cond); 4957 assert (status == 0, "invariant"); 4958 status = pthread_mutex_unlock(_mutex); 4959 assert (status == 0, "invariant"); 4960 } else { 4961 status = pthread_mutex_unlock(_mutex); 4962 assert (status == 0, "invariant"); 4963 status = pthread_cond_signal (_cond); 4964 assert (status == 0, "invariant"); 4965 } 4966 } else { 4967 pthread_mutex_unlock(_mutex); 4968 assert (status == 0, "invariant"); 4969 } 4970 } 4971 4972 extern char** environ; 4973 4974 // Run the specified command in a separate process. Return its exit value, 4975 // or -1 on failure (e.g. can't fork a new process). 4976 // Unlike system(), this function can be called from signal handler. It 4977 // doesn't block SIGINT et al. 4978 int os::fork_and_exec(char* cmd) { 4979 char * argv[4] = {"sh", "-c", cmd, NULL}; 4980 4981 pid_t pid = fork(); 4982 4983 if (pid < 0) { 4984 // fork failed 4985 return -1; 4986 4987 } else if (pid == 0) { 4988 // child process 4989 4990 // Try to be consistent with system(), which uses "/usr/bin/sh" on AIX. 4991 execve("/usr/bin/sh", argv, environ); 4992 4993 // execve failed 4994 _exit(-1); 4995 4996 } else { 4997 // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't 4998 // care about the actual exit code, for now. 4999 5000 int status; 5001 5002 // Wait for the child process to exit. This returns immediately if 5003 // the child has already exited. */ 5004 while (waitpid(pid, &status, 0) < 0) { 5005 switch (errno) { 5006 case ECHILD: return 0; 5007 case EINTR: break; 5008 default: return -1; 5009 } 5010 } 5011 5012 if (WIFEXITED(status)) { 5013 // The child exited normally; get its exit code. 5014 return WEXITSTATUS(status); 5015 } else if (WIFSIGNALED(status)) { 5016 // The child exited because of a signal. 5017 // The best value to return is 0x80 + signal number, 5018 // because that is what all Unix shells do, and because 5019 // it allows callers to distinguish between process exit and 5020 // process death by signal. 5021 return 0x80 + WTERMSIG(status); 5022 } else { 5023 // Unknown exit code; pass it through. 5024 return status; 5025 } 5026 } 5027 return -1; 5028 } 5029 5030 // is_headless_jre() 5031 // 5032 // Test for the existence of xawt/libmawt.so or libawt_xawt.so 5033 // in order to report if we are running in a headless jre. 5034 // 5035 // Since JDK8 xawt/libmawt.so is moved into the same directory 5036 // as libawt.so, and renamed libawt_xawt.so 5037 bool os::is_headless_jre() { 5038 struct stat statbuf; 5039 char buf[MAXPATHLEN]; 5040 char libmawtpath[MAXPATHLEN]; 5041 const char *xawtstr = "/xawt/libmawt.so"; 5042 const char *new_xawtstr = "/libawt_xawt.so"; 5043 5044 char *p; 5045 5046 // Get path to libjvm.so 5047 os::jvm_path(buf, sizeof(buf)); 5048 5049 // Get rid of libjvm.so 5050 p = strrchr(buf, '/'); 5051 if (p == NULL) return false; 5052 else *p = '\0'; 5053 5054 // Get rid of client or server 5055 p = strrchr(buf, '/'); 5056 if (p == NULL) return false; 5057 else *p = '\0'; 5058 5059 // check xawt/libmawt.so 5060 strcpy(libmawtpath, buf); 5061 strcat(libmawtpath, xawtstr); 5062 if (::stat(libmawtpath, &statbuf) == 0) return false; 5063 5064 // check libawt_xawt.so 5065 strcpy(libmawtpath, buf); 5066 strcat(libmawtpath, new_xawtstr); 5067 if (::stat(libmawtpath, &statbuf) == 0) return false; 5068 5069 return true; 5070 } 5071 5072 // Get the default path to the core file 5073 // Returns the length of the string 5074 int os::get_core_path(char* buffer, size_t bufferSize) { 5075 const char* p = get_current_directory(buffer, bufferSize); 5076 5077 if (p == NULL) { 5078 assert(p != NULL, "failed to get current directory"); 5079 return 0; 5080 } 5081 5082 jio_snprintf(buffer, bufferSize, "%s/core or core.%d", 5083 p, current_process_id()); 5084 5085 return strlen(buffer); 5086 } 5087 5088 #ifndef PRODUCT 5089 void TestReserveMemorySpecial_test() { 5090 // No tests available for this platform 5091 } 5092 #endif