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