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