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