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