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