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