1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "jvm_solaris.h"
  34 #include "memory/allocation.inline.hpp"
  35 #include "memory/filemap.hpp"
  36 #include "mutex_solaris.inline.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "os_share_solaris.hpp"
  39 #include "prims/jniFastGetField.hpp"
  40 #include "prims/jvm.h"
  41 #include "prims/jvm_misc.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/extendedPC.hpp"
  44 #include "runtime/globals.hpp"
  45 #include "runtime/interfaceSupport.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/mutexLocker.hpp"
  49 #include "runtime/objectMonitor.hpp"
  50 #include "runtime/osThread.hpp"
  51 #include "runtime/perfMemory.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/statSampler.hpp"
  54 #include "runtime/stubRoutines.hpp"
  55 #include "runtime/threadCritical.hpp"
  56 #include "runtime/timer.hpp"
  57 #include "services/attachListener.hpp"
  58 #include "services/memTracker.hpp"
  59 #include "services/runtimeService.hpp"
  60 #include "thread_solaris.inline.hpp"
  61 #include "utilities/decoder.hpp"
  62 #include "utilities/defaultStream.hpp"
  63 #include "utilities/events.hpp"
  64 #include "utilities/growableArray.hpp"
  65 #include "utilities/vmError.hpp"
  66 #ifdef TARGET_ARCH_x86
  67 # include "assembler_x86.inline.hpp"
  68 # include "nativeInst_x86.hpp"
  69 #endif
  70 #ifdef TARGET_ARCH_sparc
  71 # include "assembler_sparc.inline.hpp"
  72 # include "nativeInst_sparc.hpp"
  73 #endif
  74 
  75 // put OS-includes here
  76 # include <dlfcn.h>
  77 # include <errno.h>
  78 # include <exception>
  79 # include <link.h>
  80 # include <poll.h>
  81 # include <pthread.h>
  82 # include <pwd.h>
  83 # include <schedctl.h>
  84 # include <setjmp.h>
  85 # include <signal.h>
  86 # include <stdio.h>
  87 # include <alloca.h>
  88 # include <sys/filio.h>
  89 # include <sys/ipc.h>
  90 # include <sys/lwp.h>
  91 # include <sys/machelf.h>     // for elf Sym structure used by dladdr1
  92 # include <sys/mman.h>
  93 # include <sys/processor.h>
  94 # include <sys/procset.h>
  95 # include <sys/pset.h>
  96 # include <sys/resource.h>
  97 # include <sys/shm.h>
  98 # include <sys/socket.h>
  99 # include <sys/stat.h>
 100 # include <sys/systeminfo.h>
 101 # include <sys/time.h>
 102 # include <sys/times.h>
 103 # include <sys/types.h>
 104 # include <sys/wait.h>
 105 # include <sys/utsname.h>
 106 # include <thread.h>
 107 # include <unistd.h>
 108 # include <sys/priocntl.h>
 109 # include <sys/rtpriocntl.h>
 110 # include <sys/tspriocntl.h>
 111 # include <sys/iapriocntl.h>
 112 # include <sys/fxpriocntl.h>
 113 # include <sys/loadavg.h>
 114 # include <string.h>
 115 # include <stdio.h>
 116 
 117 # define _STRUCTURED_PROC 1  //  this gets us the new structured proc interfaces of 5.6 & later
 118 # include <sys/procfs.h>     //  see comment in <sys/procfs.h>
 119 
 120 #define MAX_PATH (2 * K)
 121 
 122 // for timer info max values which include all bits
 123 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
 124 
 125 #ifdef _GNU_SOURCE
 126 // See bug #6514594
 127 extern "C" int madvise(caddr_t, size_t, int);
 128 extern "C" int memcntl(caddr_t addr, size_t len, int cmd, caddr_t arg,
 129                        int attr, int mask);
 130 #endif //_GNU_SOURCE
 131 
 132 /*
 133   MPSS Changes Start.
 134   The JVM binary needs to be built and run on pre-Solaris 9
 135   systems, but the constants needed by MPSS are only in Solaris 9
 136   header files.  They are textually replicated here to allow
 137   building on earlier systems.  Once building on Solaris 8 is
 138   no longer a requirement, these #defines can be replaced by ordinary
 139   system .h inclusion.
 140 
 141   In earlier versions of the  JDK and Solaris, we used ISM for large pages.
 142   But ISM requires shared memory to achieve this and thus has many caveats.
 143   MPSS is a fully transparent and is a cleaner way to get large pages.
 144   Although we still require keeping ISM for backward compatiblitiy as well as
 145   giving the opportunity to use large pages on older systems it is
 146   recommended that MPSS be used for Solaris 9 and above.
 147 
 148 */
 149 
 150 #ifndef MC_HAT_ADVISE
 151 
 152 struct memcntl_mha {
 153   uint_t          mha_cmd;        /* command(s) */
 154   uint_t          mha_flags;
 155   size_t          mha_pagesize;
 156 };
 157 #define MC_HAT_ADVISE   7       /* advise hat map size */
 158 #define MHA_MAPSIZE_VA  0x1     /* set preferred page size */
 159 #define MAP_ALIGN       0x200   /* addr specifies alignment */
 160 
 161 #endif
 162 // MPSS Changes End.
 163 
 164 
 165 // Here are some liblgrp types from sys/lgrp_user.h to be able to
 166 // compile on older systems without this header file.
 167 
 168 #ifndef MADV_ACCESS_LWP
 169 # define  MADV_ACCESS_LWP         7       /* next LWP to access heavily */
 170 #endif
 171 #ifndef MADV_ACCESS_MANY
 172 # define  MADV_ACCESS_MANY        8       /* many processes to access heavily */
 173 #endif
 174 
 175 #ifndef LGRP_RSRC_CPU
 176 # define LGRP_RSRC_CPU           0       /* CPU resources */
 177 #endif
 178 #ifndef LGRP_RSRC_MEM
 179 # define LGRP_RSRC_MEM           1       /* memory resources */
 180 #endif
 181 
 182 // Some more macros from sys/mman.h that are not present in Solaris 8.
 183 
 184 #ifndef MAX_MEMINFO_CNT
 185 /*
 186  * info_req request type definitions for meminfo
 187  * request types starting with MEMINFO_V are used for Virtual addresses
 188  * and should not be mixed with MEMINFO_PLGRP which is targeted for Physical
 189  * addresses
 190  */
 191 # define MEMINFO_SHIFT           16
 192 # define MEMINFO_MASK            (0xFF << MEMINFO_SHIFT)
 193 # define MEMINFO_VPHYSICAL       (0x01 << MEMINFO_SHIFT) /* get physical addr */
 194 # define MEMINFO_VLGRP           (0x02 << MEMINFO_SHIFT) /* get lgroup */
 195 # define MEMINFO_VPAGESIZE       (0x03 << MEMINFO_SHIFT) /* size of phys page */
 196 # define MEMINFO_VREPLCNT        (0x04 << MEMINFO_SHIFT) /* no. of replica */
 197 # define MEMINFO_VREPL           (0x05 << MEMINFO_SHIFT) /* physical replica */
 198 # define MEMINFO_VREPL_LGRP      (0x06 << MEMINFO_SHIFT) /* lgrp of replica */
 199 # define MEMINFO_PLGRP           (0x07 << MEMINFO_SHIFT) /* lgroup for paddr */
 200 
 201 /* maximum number of addresses meminfo() can process at a time */
 202 # define MAX_MEMINFO_CNT 256
 203 
 204 /* maximum number of request types */
 205 # define MAX_MEMINFO_REQ 31
 206 #endif
 207 
 208 // see thr_setprio(3T) for the basis of these numbers
 209 #define MinimumPriority 0
 210 #define NormalPriority  64
 211 #define MaximumPriority 127
 212 
 213 // Values for ThreadPriorityPolicy == 1
 214 int prio_policy1[CriticalPriority+1] = {
 215   -99999,  0, 16,  32,  48,  64,
 216           80, 96, 112, 124, 127, 127 };
 217 
 218 // System parameters used internally
 219 static clock_t clock_tics_per_sec = 100;
 220 
 221 // Track if we have called enable_extended_FILE_stdio (on Solaris 10u4+)
 222 static bool enabled_extended_FILE_stdio = false;
 223 
 224 // For diagnostics to print a message once. see run_periodic_checks
 225 static bool check_addr0_done = false;
 226 static sigset_t check_signal_done;
 227 static bool check_signals = true;
 228 
 229 address os::Solaris::handler_start;  // start pc of thr_sighndlrinfo
 230 address os::Solaris::handler_end;    // end pc of thr_sighndlrinfo
 231 
 232 address os::Solaris::_main_stack_base = NULL;  // 4352906 workaround
 233 
 234 
 235 // "default" initializers for missing libc APIs
 236 extern "C" {
 237   static int lwp_mutex_init(mutex_t *mx, int scope, void *arg) { memset(mx, 0, sizeof(mutex_t)); return 0; }
 238   static int lwp_mutex_destroy(mutex_t *mx)                 { return 0; }
 239 
 240   static int lwp_cond_init(cond_t *cv, int scope, void *arg){ memset(cv, 0, sizeof(cond_t)); return 0; }
 241   static int lwp_cond_destroy(cond_t *cv)                   { return 0; }
 242 }
 243 
 244 // "default" initializers for pthread-based synchronization
 245 extern "C" {
 246   static int pthread_mutex_default_init(mutex_t *mx, int scope, void *arg) { memset(mx, 0, sizeof(mutex_t)); return 0; }
 247   static int pthread_cond_default_init(cond_t *cv, int scope, void *arg){ memset(cv, 0, sizeof(cond_t)); return 0; }
 248 }
 249 
 250 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
 251 
 252 // Thread Local Storage
 253 // This is common to all Solaris platforms so it is defined here,
 254 // in this common file.
 255 // The declarations are in the os_cpu threadLS*.hpp files.
 256 //
 257 // Static member initialization for TLS
 258 Thread* ThreadLocalStorage::_get_thread_cache[ThreadLocalStorage::_pd_cache_size] = {NULL};
 259 
 260 #ifndef PRODUCT
 261 #define _PCT(n,d)       ((100.0*(double)(n))/(double)(d))
 262 
 263 int ThreadLocalStorage::_tcacheHit = 0;
 264 int ThreadLocalStorage::_tcacheMiss = 0;
 265 
 266 void ThreadLocalStorage::print_statistics() {
 267   int total = _tcacheMiss+_tcacheHit;
 268   tty->print_cr("Thread cache hits %d misses %d total %d percent %f\n",
 269                 _tcacheHit, _tcacheMiss, total, _PCT(_tcacheHit, total));
 270 }
 271 #undef _PCT
 272 #endif // PRODUCT
 273 
 274 Thread* ThreadLocalStorage::get_thread_via_cache_slowly(uintptr_t raw_id,
 275                                                         int index) {
 276   Thread *thread = get_thread_slow();
 277   if (thread != NULL) {
 278     address sp = os::current_stack_pointer();
 279     guarantee(thread->_stack_base == NULL ||
 280               (sp <= thread->_stack_base &&
 281                  sp >= thread->_stack_base - thread->_stack_size) ||
 282                is_error_reported(),
 283               "sp must be inside of selected thread stack");
 284 
 285     thread->set_self_raw_id(raw_id);  // mark for quick retrieval
 286     _get_thread_cache[ index ] = thread;
 287   }
 288   return thread;
 289 }
 290 
 291 
 292 static const double all_zero[ sizeof(Thread) / sizeof(double) + 1 ] = {0};
 293 #define NO_CACHED_THREAD ((Thread*)all_zero)
 294 
 295 void ThreadLocalStorage::pd_set_thread(Thread* thread) {
 296 
 297   // Store the new value before updating the cache to prevent a race
 298   // between get_thread_via_cache_slowly() and this store operation.
 299   os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
 300 
 301   // Update thread cache with new thread if setting on thread create,
 302   // or NO_CACHED_THREAD (zeroed) thread if resetting thread on exit.
 303   uintptr_t raw = pd_raw_thread_id();
 304   int ix = pd_cache_index(raw);
 305   _get_thread_cache[ix] = thread == NULL ? NO_CACHED_THREAD : thread;
 306 }
 307 
 308 void ThreadLocalStorage::pd_init() {
 309   for (int i = 0; i < _pd_cache_size; i++) {
 310     _get_thread_cache[i] = NO_CACHED_THREAD;
 311   }
 312 }
 313 
 314 // Invalidate all the caches (happens to be the same as pd_init).
 315 void ThreadLocalStorage::pd_invalidate_all() { pd_init(); }
 316 
 317 #undef NO_CACHED_THREAD
 318 
 319 // END Thread Local Storage
 320 
 321 static inline size_t adjust_stack_size(address base, size_t size) {
 322   if ((ssize_t)size < 0) {
 323     // 4759953: Compensate for ridiculous stack size.
 324     size = max_intx;
 325   }
 326   if (size > (size_t)base) {
 327     // 4812466: Make sure size doesn't allow the stack to wrap the address space.
 328     size = (size_t)base;
 329   }
 330   return size;
 331 }
 332 
 333 static inline stack_t get_stack_info() {
 334   stack_t st;
 335   int retval = thr_stksegment(&st);
 336   st.ss_size = adjust_stack_size((address)st.ss_sp, st.ss_size);
 337   assert(retval == 0, "incorrect return value from thr_stksegment");
 338   assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
 339   assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
 340   return st;
 341 }
 342 
 343 address os::current_stack_base() {
 344   int r = thr_main() ;
 345   guarantee (r == 0 || r == 1, "CR6501650 or CR6493689") ;
 346   bool is_primordial_thread = r;
 347 
 348   // Workaround 4352906, avoid calls to thr_stksegment by
 349   // thr_main after the first one (it looks like we trash
 350   // some data, causing the value for ss_sp to be incorrect).
 351   if (!is_primordial_thread || os::Solaris::_main_stack_base == NULL) {
 352     stack_t st = get_stack_info();
 353     if (is_primordial_thread) {
 354       // cache initial value of stack base
 355       os::Solaris::_main_stack_base = (address)st.ss_sp;
 356     }
 357     return (address)st.ss_sp;
 358   } else {
 359     guarantee(os::Solaris::_main_stack_base != NULL, "Attempt to use null cached stack base");
 360     return os::Solaris::_main_stack_base;
 361   }
 362 }
 363 
 364 size_t os::current_stack_size() {
 365   size_t size;
 366 
 367   int r = thr_main() ;
 368   guarantee (r == 0 || r == 1, "CR6501650 or CR6493689") ;
 369   if(!r) {
 370     size = get_stack_info().ss_size;
 371   } else {
 372     struct rlimit limits;
 373     getrlimit(RLIMIT_STACK, &limits);
 374     size = adjust_stack_size(os::Solaris::_main_stack_base, (size_t)limits.rlim_cur);
 375   }
 376   // base may not be page aligned
 377   address base = current_stack_base();
 378   address bottom = (address)align_size_up((intptr_t)(base - size), os::vm_page_size());;
 379   return (size_t)(base - bottom);
 380 }
 381 
 382 struct tm* os::localtime_pd(const time_t* clock, struct tm*  res) {
 383   return localtime_r(clock, res);
 384 }
 385 
 386 // interruptible infrastructure
 387 
 388 // setup_interruptible saves the thread state before going into an
 389 // interruptible system call.
 390 // The saved state is used to restore the thread to
 391 // its former state whether or not an interrupt is received.
 392 // Used by classloader os::read
 393 // os::restartable_read calls skip this layer and stay in _thread_in_native
 394 
 395 void os::Solaris::setup_interruptible(JavaThread* thread) {
 396 
 397   JavaThreadState thread_state = thread->thread_state();
 398 
 399   assert(thread_state != _thread_blocked, "Coming from the wrong thread");
 400   assert(thread_state != _thread_in_native, "Native threads skip setup_interruptible");
 401   OSThread* osthread = thread->osthread();
 402   osthread->set_saved_interrupt_thread_state(thread_state);
 403   thread->frame_anchor()->make_walkable(thread);
 404   ThreadStateTransition::transition(thread, thread_state, _thread_blocked);
 405 }
 406 
 407 // Version of setup_interruptible() for threads that are already in
 408 // _thread_blocked. Used by os_sleep().
 409 void os::Solaris::setup_interruptible_already_blocked(JavaThread* thread) {
 410   thread->frame_anchor()->make_walkable(thread);
 411 }
 412 
 413 JavaThread* os::Solaris::setup_interruptible() {
 414   JavaThread* thread = (JavaThread*)ThreadLocalStorage::thread();
 415   setup_interruptible(thread);
 416   return thread;
 417 }
 418 
 419 void os::Solaris::try_enable_extended_io() {
 420   typedef int (*enable_extended_FILE_stdio_t)(int, int);
 421 
 422   if (!UseExtendedFileIO) {
 423     return;
 424   }
 425 
 426   enable_extended_FILE_stdio_t enabler =
 427     (enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT,
 428                                          "enable_extended_FILE_stdio");
 429   if (enabler) {
 430     enabler(-1, -1);
 431   }
 432 }
 433 
 434 
 435 #ifdef ASSERT
 436 
 437 JavaThread* os::Solaris::setup_interruptible_native() {
 438   JavaThread* thread = (JavaThread*)ThreadLocalStorage::thread();
 439   JavaThreadState thread_state = thread->thread_state();
 440   assert(thread_state == _thread_in_native, "Assumed thread_in_native");
 441   return thread;
 442 }
 443 
 444 void os::Solaris::cleanup_interruptible_native(JavaThread* thread) {
 445   JavaThreadState thread_state = thread->thread_state();
 446   assert(thread_state == _thread_in_native, "Assumed thread_in_native");
 447 }
 448 #endif
 449 
 450 // cleanup_interruptible reverses the effects of setup_interruptible
 451 // setup_interruptible_already_blocked() does not need any cleanup.
 452 
 453 void os::Solaris::cleanup_interruptible(JavaThread* thread) {
 454   OSThread* osthread = thread->osthread();
 455 
 456   ThreadStateTransition::transition(thread, _thread_blocked, osthread->saved_interrupt_thread_state());
 457 }
 458 
 459 // I/O interruption related counters called in _INTERRUPTIBLE
 460 
 461 void os::Solaris::bump_interrupted_before_count() {
 462   RuntimeService::record_interrupted_before_count();
 463 }
 464 
 465 void os::Solaris::bump_interrupted_during_count() {
 466   RuntimeService::record_interrupted_during_count();
 467 }
 468 
 469 static int _processors_online = 0;
 470 
 471          jint os::Solaris::_os_thread_limit = 0;
 472 volatile jint os::Solaris::_os_thread_count = 0;
 473 
 474 julong os::available_memory() {
 475   return Solaris::available_memory();
 476 }
 477 
 478 julong os::Solaris::available_memory() {
 479   return (julong)sysconf(_SC_AVPHYS_PAGES) * os::vm_page_size();
 480 }
 481 
 482 julong os::Solaris::_physical_memory = 0;
 483 
 484 julong os::physical_memory() {
 485    return Solaris::physical_memory();
 486 }
 487 
 488 julong os::allocatable_physical_memory(julong size) {
 489 #ifdef _LP64
 490    return size;
 491 #else
 492    julong result = MIN2(size, (julong)3835*M);
 493    if (!is_allocatable(result)) {
 494      // Memory allocations will be aligned but the alignment
 495      // is not known at this point.  Alignments will
 496      // be at most to LargePageSizeInBytes.  Protect
 497      // allocations from alignments up to illegal
 498      // values. If at this point 2G is illegal.
 499      julong reasonable_size = (julong)2*G - 2 * LargePageSizeInBytes;
 500      result =  MIN2(size, reasonable_size);
 501    }
 502    return result;
 503 #endif
 504 }
 505 
 506 static hrtime_t first_hrtime = 0;
 507 static const hrtime_t hrtime_hz = 1000*1000*1000;
 508 const int LOCK_BUSY = 1;
 509 const int LOCK_FREE = 0;
 510 const int LOCK_INVALID = -1;
 511 static volatile hrtime_t max_hrtime = 0;
 512 static volatile int max_hrtime_lock = LOCK_FREE;     // Update counter with LSB as lock-in-progress
 513 
 514 
 515 void os::Solaris::initialize_system_info() {
 516   set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
 517   _processors_online = sysconf (_SC_NPROCESSORS_ONLN);
 518   _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
 519 }
 520 
 521 int os::active_processor_count() {
 522   int online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 523   pid_t pid = getpid();
 524   psetid_t pset = PS_NONE;
 525   // Are we running in a processor set or is there any processor set around?
 526   if (pset_bind(PS_QUERY, P_PID, pid, &pset) == 0) {
 527     uint_t pset_cpus;
 528     // Query the number of cpus available to us.
 529     if (pset_info(pset, NULL, &pset_cpus, NULL) == 0) {
 530       assert(pset_cpus > 0 && pset_cpus <= online_cpus, "sanity check");
 531       _processors_online = pset_cpus;
 532       return pset_cpus;
 533     }
 534   }
 535   // Otherwise return number of online cpus
 536   return online_cpus;
 537 }
 538 
 539 static bool find_processors_in_pset(psetid_t        pset,
 540                                     processorid_t** id_array,
 541                                     uint_t*         id_length) {
 542   bool result = false;
 543   // Find the number of processors in the processor set.
 544   if (pset_info(pset, NULL, id_length, NULL) == 0) {
 545     // Make up an array to hold their ids.
 546     *id_array = NEW_C_HEAP_ARRAY(processorid_t, *id_length, mtInternal);
 547     // Fill in the array with their processor ids.
 548     if (pset_info(pset, NULL, id_length, *id_array) == 0) {
 549       result = true;
 550     }
 551   }
 552   return result;
 553 }
 554 
 555 // Callers of find_processors_online() must tolerate imprecise results --
 556 // the system configuration can change asynchronously because of DR
 557 // or explicit psradm operations.
 558 //
 559 // We also need to take care that the loop (below) terminates as the
 560 // number of processors online can change between the _SC_NPROCESSORS_ONLN
 561 // request and the loop that builds the list of processor ids.   Unfortunately
 562 // there's no reliable way to determine the maximum valid processor id,
 563 // so we use a manifest constant, MAX_PROCESSOR_ID, instead.  See p_online
 564 // man pages, which claim the processor id set is "sparse, but
 565 // not too sparse".  MAX_PROCESSOR_ID is used to ensure that we eventually
 566 // exit the loop.
 567 //
 568 // In the future we'll be able to use sysconf(_SC_CPUID_MAX), but that's
 569 // not available on S8.0.
 570 
 571 static bool find_processors_online(processorid_t** id_array,
 572                                    uint*           id_length) {
 573   const processorid_t MAX_PROCESSOR_ID = 100000 ;
 574   // Find the number of processors online.
 575   *id_length = sysconf(_SC_NPROCESSORS_ONLN);
 576   // Make up an array to hold their ids.
 577   *id_array = NEW_C_HEAP_ARRAY(processorid_t, *id_length, mtInternal);
 578   // Processors need not be numbered consecutively.
 579   long found = 0;
 580   processorid_t next = 0;
 581   while (found < *id_length && next < MAX_PROCESSOR_ID) {
 582     processor_info_t info;
 583     if (processor_info(next, &info) == 0) {
 584       // NB, PI_NOINTR processors are effectively online ...
 585       if (info.pi_state == P_ONLINE || info.pi_state == P_NOINTR) {
 586         (*id_array)[found] = next;
 587         found += 1;
 588       }
 589     }
 590     next += 1;
 591   }
 592   if (found < *id_length) {
 593       // The loop above didn't identify the expected number of processors.
 594       // We could always retry the operation, calling sysconf(_SC_NPROCESSORS_ONLN)
 595       // and re-running the loop, above, but there's no guarantee of progress
 596       // if the system configuration is in flux.  Instead, we just return what
 597       // we've got.  Note that in the worst case find_processors_online() could
 598       // return an empty set.  (As a fall-back in the case of the empty set we
 599       // could just return the ID of the current processor).
 600       *id_length = found ;
 601   }
 602 
 603   return true;
 604 }
 605 
 606 static bool assign_distribution(processorid_t* id_array,
 607                                 uint           id_length,
 608                                 uint*          distribution,
 609                                 uint           distribution_length) {
 610   // We assume we can assign processorid_t's to uint's.
 611   assert(sizeof(processorid_t) == sizeof(uint),
 612          "can't convert processorid_t to uint");
 613   // Quick check to see if we won't succeed.
 614   if (id_length < distribution_length) {
 615     return false;
 616   }
 617   // Assign processor ids to the distribution.
 618   // Try to shuffle processors to distribute work across boards,
 619   // assuming 4 processors per board.
 620   const uint processors_per_board = ProcessDistributionStride;
 621   // Find the maximum processor id.
 622   processorid_t max_id = 0;
 623   for (uint m = 0; m < id_length; m += 1) {
 624     max_id = MAX2(max_id, id_array[m]);
 625   }
 626   // The next id, to limit loops.
 627   const processorid_t limit_id = max_id + 1;
 628   // Make up markers for available processors.
 629   bool* available_id = NEW_C_HEAP_ARRAY(bool, limit_id, mtInternal);
 630   for (uint c = 0; c < limit_id; c += 1) {
 631     available_id[c] = false;
 632   }
 633   for (uint a = 0; a < id_length; a += 1) {
 634     available_id[id_array[a]] = true;
 635   }
 636   // Step by "boards", then by "slot", copying to "assigned".
 637   // NEEDS_CLEANUP: The assignment of processors should be stateful,
 638   //                remembering which processors have been assigned by
 639   //                previous calls, etc., so as to distribute several
 640   //                independent calls of this method.  What we'd like is
 641   //                It would be nice to have an API that let us ask
 642   //                how many processes are bound to a processor,
 643   //                but we don't have that, either.
 644   //                In the short term, "board" is static so that
 645   //                subsequent distributions don't all start at board 0.
 646   static uint board = 0;
 647   uint assigned = 0;
 648   // Until we've found enough processors ....
 649   while (assigned < distribution_length) {
 650     // ... find the next available processor in the board.
 651     for (uint slot = 0; slot < processors_per_board; slot += 1) {
 652       uint try_id = board * processors_per_board + slot;
 653       if ((try_id < limit_id) && (available_id[try_id] == true)) {
 654         distribution[assigned] = try_id;
 655         available_id[try_id] = false;
 656         assigned += 1;
 657         break;
 658       }
 659     }
 660     board += 1;
 661     if (board * processors_per_board + 0 >= limit_id) {
 662       board = 0;
 663     }
 664   }
 665   if (available_id != NULL) {
 666     FREE_C_HEAP_ARRAY(bool, available_id, mtInternal);
 667   }
 668   return true;
 669 }
 670 
 671 void os::set_native_thread_name(const char *name) {
 672   // Not yet implemented.
 673   return;
 674 }
 675 
 676 bool os::distribute_processes(uint length, uint* distribution) {
 677   bool result = false;
 678   // Find the processor id's of all the available CPUs.
 679   processorid_t* id_array  = NULL;
 680   uint           id_length = 0;
 681   // There are some races between querying information and using it,
 682   // since processor sets can change dynamically.
 683   psetid_t pset = PS_NONE;
 684   // Are we running in a processor set?
 685   if ((pset_bind(PS_QUERY, P_PID, P_MYID, &pset) == 0) && pset != PS_NONE) {
 686     result = find_processors_in_pset(pset, &id_array, &id_length);
 687   } else {
 688     result = find_processors_online(&id_array, &id_length);
 689   }
 690   if (result == true) {
 691     if (id_length >= length) {
 692       result = assign_distribution(id_array, id_length, distribution, length);
 693     } else {
 694       result = false;
 695     }
 696   }
 697   if (id_array != NULL) {
 698     FREE_C_HEAP_ARRAY(processorid_t, id_array, mtInternal);
 699   }
 700   return result;
 701 }
 702 
 703 bool os::bind_to_processor(uint processor_id) {
 704   // We assume that a processorid_t can be stored in a uint.
 705   assert(sizeof(uint) == sizeof(processorid_t),
 706          "can't convert uint to processorid_t");
 707   int bind_result =
 708     processor_bind(P_LWPID,                       // bind LWP.
 709                    P_MYID,                        // bind current LWP.
 710                    (processorid_t) processor_id,  // id.
 711                    NULL);                         // don't return old binding.
 712   return (bind_result == 0);
 713 }
 714 
 715 bool os::getenv(const char* name, char* buffer, int len) {
 716   char* val = ::getenv( name );
 717   if ( val == NULL
 718   ||   strlen(val) + 1  >  len ) {
 719     if (len > 0)  buffer[0] = 0; // return a null string
 720     return false;
 721   }
 722   strcpy( buffer, val );
 723   return true;
 724 }
 725 
 726 
 727 // Return true if user is running as root.
 728 
 729 bool os::have_special_privileges() {
 730   static bool init = false;
 731   static bool privileges = false;
 732   if (!init) {
 733     privileges = (getuid() != geteuid()) || (getgid() != getegid());
 734     init = true;
 735   }
 736   return privileges;
 737 }
 738 
 739 
 740 void os::init_system_properties_values() {
 741   char arch[12];
 742   sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
 743 
 744   // The next steps are taken in the product version:
 745   //
 746   // Obtain the JAVA_HOME value from the location of libjvm[_g].so.
 747   // This library should be located at:
 748   // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm[_g].so.
 749   //
 750   // If "/jre/lib/" appears at the right place in the path, then we
 751   // assume libjvm[_g].so is installed in a JDK and we use this path.
 752   //
 753   // Otherwise exit with message: "Could not create the Java virtual machine."
 754   //
 755   // The following extra steps are taken in the debugging version:
 756   //
 757   // If "/jre/lib/" does NOT appear at the right place in the path
 758   // instead of exit check for $JAVA_HOME environment variable.
 759   //
 760   // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
 761   // then we append a fake suffix "hotspot/libjvm[_g].so" to this path so
 762   // it looks like libjvm[_g].so is installed there
 763   // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm[_g].so.
 764   //
 765   // Otherwise exit.
 766   //
 767   // Important note: if the location of libjvm.so changes this
 768   // code needs to be changed accordingly.
 769 
 770   // The next few definitions allow the code to be verbatim:
 771 #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n), mtInternal)
 772 #define free(p) FREE_C_HEAP_ARRAY(char, p, mtInternal)
 773 #define getenv(n) ::getenv(n)
 774 
 775 #define EXTENSIONS_DIR  "/lib/ext"
 776 #define ENDORSED_DIR    "/lib/endorsed"
 777 #define COMMON_DIR      "/usr/jdk/packages"
 778 
 779   {
 780     /* sysclasspath, java_home, dll_dir */
 781     {
 782         char *home_path;
 783         char *dll_path;
 784         char *pslash;
 785         char buf[MAXPATHLEN];
 786         os::jvm_path(buf, sizeof(buf));
 787 
 788         // Found the full path to libjvm.so.
 789         // Now cut the path to <java_home>/jre if we can.
 790         *(strrchr(buf, '/')) = '\0';  /* get rid of /libjvm.so */
 791         pslash = strrchr(buf, '/');
 792         if (pslash != NULL)
 793             *pslash = '\0';           /* get rid of /{client|server|hotspot} */
 794         dll_path = malloc(strlen(buf) + 1);
 795         if (dll_path == NULL)
 796             return;
 797         strcpy(dll_path, buf);
 798         Arguments::set_dll_dir(dll_path);
 799 
 800         if (pslash != NULL) {
 801             pslash = strrchr(buf, '/');
 802             if (pslash != NULL) {
 803                 *pslash = '\0';       /* get rid of /<arch> */
 804                 pslash = strrchr(buf, '/');
 805                 if (pslash != NULL)
 806                     *pslash = '\0';   /* get rid of /lib */
 807             }
 808         }
 809 
 810         home_path = malloc(strlen(buf) + 1);
 811         if (home_path == NULL)
 812             return;
 813         strcpy(home_path, buf);
 814         Arguments::set_java_home(home_path);
 815 
 816         if (!set_boot_path('/', ':'))
 817             return;
 818     }
 819 
 820     /*
 821      * Where to look for native libraries
 822      */
 823     {
 824       // Use dlinfo() to determine the correct java.library.path.
 825       //
 826       // If we're launched by the Java launcher, and the user
 827       // does not set java.library.path explicitly on the commandline,
 828       // the Java launcher sets LD_LIBRARY_PATH for us and unsets
 829       // LD_LIBRARY_PATH_32 and LD_LIBRARY_PATH_64.  In this case
 830       // dlinfo returns LD_LIBRARY_PATH + crle settings (including
 831       // /usr/lib), which is exactly what we want.
 832       //
 833       // If the user does set java.library.path, it completely
 834       // overwrites this setting, and always has.
 835       //
 836       // If we're not launched by the Java launcher, we may
 837       // get here with any/all of the LD_LIBRARY_PATH[_32|64]
 838       // settings.  Again, dlinfo does exactly what we want.
 839 
 840       Dl_serinfo     _info, *info = &_info;
 841       Dl_serpath     *path;
 842       char*          library_path;
 843       char           *common_path;
 844       int            i;
 845 
 846       // determine search path count and required buffer size
 847       if (dlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info) == -1) {
 848         vm_exit_during_initialization("dlinfo SERINFOSIZE request", dlerror());
 849       }
 850 
 851       // allocate new buffer and initialize
 852       info = (Dl_serinfo*)malloc(_info.dls_size);
 853       if (info == NULL) {
 854         vm_exit_out_of_memory(_info.dls_size,
 855                               "init_system_properties_values info");
 856       }
 857       info->dls_size = _info.dls_size;
 858       info->dls_cnt = _info.dls_cnt;
 859 
 860       // obtain search path information
 861       if (dlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info) == -1) {
 862         free(info);
 863         vm_exit_during_initialization("dlinfo SERINFO request", dlerror());
 864       }
 865 
 866       path = &info->dls_serpath[0];
 867 
 868       // Note: Due to a legacy implementation, most of the library path
 869       // is set in the launcher.  This was to accomodate linking restrictions
 870       // on legacy Solaris implementations (which are no longer supported).
 871       // Eventually, all the library path setting will be done here.
 872       //
 873       // However, to prevent the proliferation of improperly built native
 874       // libraries, the new path component /usr/jdk/packages is added here.
 875 
 876       // Determine the actual CPU architecture.
 877       char cpu_arch[12];
 878       sysinfo(SI_ARCHITECTURE, cpu_arch, sizeof(cpu_arch));
 879 #ifdef _LP64
 880       // If we are a 64-bit vm, perform the following translations:
 881       //   sparc   -> sparcv9
 882       //   i386    -> amd64
 883       if (strcmp(cpu_arch, "sparc") == 0)
 884         strcat(cpu_arch, "v9");
 885       else if (strcmp(cpu_arch, "i386") == 0)
 886         strcpy(cpu_arch, "amd64");
 887 #endif
 888 
 889       // Construct the invariant part of ld_library_path. Note that the
 890       // space for the colon and the trailing null are provided by the
 891       // nulls included by the sizeof operator.
 892       size_t bufsize = sizeof(COMMON_DIR) + sizeof("/lib/") + strlen(cpu_arch);
 893       common_path = malloc(bufsize);
 894       if (common_path == NULL) {
 895         free(info);
 896         vm_exit_out_of_memory(bufsize,
 897                               "init_system_properties_values common_path");
 898       }
 899       sprintf(common_path, COMMON_DIR "/lib/%s", cpu_arch);
 900 
 901       // struct size is more than sufficient for the path components obtained
 902       // through the dlinfo() call, so only add additional space for the path
 903       // components explicitly added here.
 904       bufsize = info->dls_size + strlen(common_path);
 905       library_path = malloc(bufsize);
 906       if (library_path == NULL) {
 907         free(info);
 908         free(common_path);
 909         vm_exit_out_of_memory(bufsize,
 910                               "init_system_properties_values library_path");
 911       }
 912       library_path[0] = '\0';
 913 
 914       // Construct the desired Java library path from the linker's library
 915       // search path.
 916       //
 917       // For compatibility, it is optimal that we insert the additional path
 918       // components specific to the Java VM after those components specified
 919       // in LD_LIBRARY_PATH (if any) but before those added by the ld.so
 920       // infrastructure.
 921       if (info->dls_cnt == 0) { // Not sure this can happen, but allow for it
 922         strcpy(library_path, common_path);
 923       } else {
 924         int inserted = 0;
 925         for (i = 0; i < info->dls_cnt; i++, path++) {
 926           uint_t flags = path->dls_flags & LA_SER_MASK;
 927           if (((flags & LA_SER_LIBPATH) == 0) && !inserted) {
 928             strcat(library_path, common_path);
 929             strcat(library_path, os::path_separator());
 930             inserted = 1;
 931           }
 932           strcat(library_path, path->dls_name);
 933           strcat(library_path, os::path_separator());
 934         }
 935         // eliminate trailing path separator
 936         library_path[strlen(library_path)-1] = '\0';
 937       }
 938 
 939       // happens before argument parsing - can't use a trace flag
 940       // tty->print_raw("init_system_properties_values: native lib path: ");
 941       // tty->print_raw_cr(library_path);
 942 
 943       // callee copies into its own buffer
 944       Arguments::set_library_path(library_path);
 945 
 946       free(common_path);
 947       free(library_path);
 948       free(info);
 949     }
 950 
 951     /*
 952      * Extensions directories.
 953      *
 954      * Note that the space for the colon and the trailing null are provided
 955      * by the nulls included by the sizeof operator (so actually one byte more
 956      * than necessary is allocated).
 957      */
 958     {
 959         char *buf = (char *) malloc(strlen(Arguments::get_java_home()) +
 960             sizeof(EXTENSIONS_DIR) + sizeof(COMMON_DIR) +
 961             sizeof(EXTENSIONS_DIR));
 962         sprintf(buf, "%s" EXTENSIONS_DIR ":" COMMON_DIR EXTENSIONS_DIR,
 963             Arguments::get_java_home());
 964         Arguments::set_ext_dirs(buf);
 965     }
 966 
 967     /* Endorsed standards default directory. */
 968     {
 969         char * buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
 970         sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
 971         Arguments::set_endorsed_dirs(buf);
 972     }
 973   }
 974 
 975 #undef malloc
 976 #undef free
 977 #undef getenv
 978 #undef EXTENSIONS_DIR
 979 #undef ENDORSED_DIR
 980 #undef COMMON_DIR
 981 
 982 }
 983 
 984 void os::breakpoint() {
 985   BREAKPOINT;
 986 }
 987 
 988 bool os::obsolete_option(const JavaVMOption *option)
 989 {
 990   if (!strncmp(option->optionString, "-Xt", 3)) {
 991     return true;
 992   } else if (!strncmp(option->optionString, "-Xtm", 4)) {
 993     return true;
 994   } else if (!strncmp(option->optionString, "-Xverifyheap", 12)) {
 995     return true;
 996   } else if (!strncmp(option->optionString, "-Xmaxjitcodesize", 16)) {
 997     return true;
 998   }
 999   return false;
1000 }
1001 
1002 bool os::Solaris::valid_stack_address(Thread* thread, address sp) {
1003   address  stackStart  = (address)thread->stack_base();
1004   address  stackEnd    = (address)(stackStart - (address)thread->stack_size());
1005   if (sp < stackStart && sp >= stackEnd ) return true;
1006   return false;
1007 }
1008 
1009 extern "C" void breakpoint() {
1010   // use debugger to set breakpoint here
1011 }
1012 
1013 static thread_t main_thread;
1014 
1015 // Thread start routine for all new Java threads
1016 extern "C" void* java_start(void* thread_addr) {
1017   // Try to randomize the cache line index of hot stack frames.
1018   // This helps when threads of the same stack traces evict each other's
1019   // cache lines. The threads can be either from the same JVM instance, or
1020   // from different JVM instances. The benefit is especially true for
1021   // processors with hyperthreading technology.
1022   static int counter = 0;
1023   int pid = os::current_process_id();
1024   alloca(((pid ^ counter++) & 7) * 128);
1025 
1026   int prio;
1027   Thread* thread = (Thread*)thread_addr;
1028   OSThread* osthr = thread->osthread();
1029 
1030   osthr->set_lwp_id( _lwp_self() );  // Store lwp in case we are bound
1031   thread->_schedctl = (void *) schedctl_init () ;
1032 
1033   if (UseNUMA) {
1034     int lgrp_id = os::numa_get_group_id();
1035     if (lgrp_id != -1) {
1036       thread->set_lgrp_id(lgrp_id);
1037     }
1038   }
1039 
1040   // If the creator called set priority before we started,
1041   // we need to call set_native_priority now that we have an lwp.
1042   // We used to get the priority from thr_getprio (we called
1043   // thr_setprio way back in create_thread) and pass it to
1044   // set_native_priority, but Solaris scales the priority
1045   // in java_to_os_priority, so when we read it back here,
1046   // we pass trash to set_native_priority instead of what's
1047   // in java_to_os_priority. So we save the native priority
1048   // in the osThread and recall it here.
1049 
1050   if ( osthr->thread_id() != -1 ) {
1051     if ( UseThreadPriorities ) {
1052       int prio = osthr->native_priority();
1053       if (ThreadPriorityVerbose) {
1054         tty->print_cr("Starting Thread " INTPTR_FORMAT ", LWP is "
1055                       INTPTR_FORMAT ", setting priority: %d\n",
1056                       osthr->thread_id(), osthr->lwp_id(), prio);
1057       }
1058       os::set_native_priority(thread, prio);
1059     }
1060   } else if (ThreadPriorityVerbose) {
1061     warning("Can't set priority in _start routine, thread id hasn't been set\n");
1062   }
1063 
1064   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
1065 
1066   // initialize signal mask for this thread
1067   os::Solaris::hotspot_sigmask(thread);
1068 
1069   thread->run();
1070 
1071   // One less thread is executing
1072   // When the VMThread gets here, the main thread may have already exited
1073   // which frees the CodeHeap containing the Atomic::dec code
1074   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
1075     Atomic::dec(&os::Solaris::_os_thread_count);
1076   }
1077 
1078   if (UseDetachedThreads) {
1079     thr_exit(NULL);
1080     ShouldNotReachHere();
1081   }
1082   return NULL;
1083 }
1084 
1085 static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
1086   // Allocate the OSThread object
1087   OSThread* osthread = new OSThread(NULL, NULL);
1088   if (osthread == NULL) return NULL;
1089 
1090   // Store info on the Solaris thread into the OSThread
1091   osthread->set_thread_id(thread_id);
1092   osthread->set_lwp_id(_lwp_self());
1093   thread->_schedctl = (void *) schedctl_init () ;
1094 
1095   if (UseNUMA) {
1096     int lgrp_id = os::numa_get_group_id();
1097     if (lgrp_id != -1) {
1098       thread->set_lgrp_id(lgrp_id);
1099     }
1100   }
1101 
1102   if ( ThreadPriorityVerbose ) {
1103     tty->print_cr("In create_os_thread, Thread " INTPTR_FORMAT ", LWP is " INTPTR_FORMAT "\n",
1104                   osthread->thread_id(), osthread->lwp_id() );
1105   }
1106 
1107   // Initial thread state is INITIALIZED, not SUSPENDED
1108   osthread->set_state(INITIALIZED);
1109 
1110   return osthread;
1111 }
1112 
1113 void os::Solaris::hotspot_sigmask(Thread* thread) {
1114 
1115   //Save caller's signal mask
1116   sigset_t sigmask;
1117   thr_sigsetmask(SIG_SETMASK, NULL, &sigmask);
1118   OSThread *osthread = thread->osthread();
1119   osthread->set_caller_sigmask(sigmask);
1120 
1121   thr_sigsetmask(SIG_UNBLOCK, os::Solaris::unblocked_signals(), NULL);
1122   if (!ReduceSignalUsage) {
1123     if (thread->is_VM_thread()) {
1124       // Only the VM thread handles BREAK_SIGNAL ...
1125       thr_sigsetmask(SIG_UNBLOCK, vm_signals(), NULL);
1126     } else {
1127       // ... all other threads block BREAK_SIGNAL
1128       assert(!sigismember(vm_signals(), SIGINT), "SIGINT should not be blocked");
1129       thr_sigsetmask(SIG_BLOCK, vm_signals(), NULL);
1130     }
1131   }
1132 }
1133 
1134 bool os::create_attached_thread(JavaThread* thread) {
1135 #ifdef ASSERT
1136   thread->verify_not_published();
1137 #endif
1138   OSThread* osthread = create_os_thread(thread, thr_self());
1139   if (osthread == NULL) {
1140      return false;
1141   }
1142 
1143   // Initial thread state is RUNNABLE
1144   osthread->set_state(RUNNABLE);
1145   thread->set_osthread(osthread);
1146 
1147   // initialize signal mask for this thread
1148   // and save the caller's signal mask
1149   os::Solaris::hotspot_sigmask(thread);
1150 
1151   return true;
1152 }
1153 
1154 bool os::create_main_thread(JavaThread* thread) {
1155 #ifdef ASSERT
1156   thread->verify_not_published();
1157 #endif
1158   if (_starting_thread == NULL) {
1159     _starting_thread = create_os_thread(thread, main_thread);
1160      if (_starting_thread == NULL) {
1161         return false;
1162      }
1163   }
1164 
1165   // The primodial thread is runnable from the start
1166   _starting_thread->set_state(RUNNABLE);
1167 
1168   thread->set_osthread(_starting_thread);
1169 
1170   // initialize signal mask for this thread
1171   // and save the caller's signal mask
1172   os::Solaris::hotspot_sigmask(thread);
1173 
1174   return true;
1175 }
1176 
1177 // _T2_libthread is true if we believe we are running with the newer
1178 // SunSoft lwp/libthread.so (2.8 patch, 2.9 default)
1179 bool os::Solaris::_T2_libthread = false;
1180 
1181 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
1182   // Allocate the OSThread object
1183   OSThread* osthread = new OSThread(NULL, NULL);
1184   if (osthread == NULL) {
1185     return false;
1186   }
1187 
1188   if ( ThreadPriorityVerbose ) {
1189     char *thrtyp;
1190     switch ( thr_type ) {
1191       case vm_thread:
1192         thrtyp = (char *)"vm";
1193         break;
1194       case cgc_thread:
1195         thrtyp = (char *)"cgc";
1196         break;
1197       case pgc_thread:
1198         thrtyp = (char *)"pgc";
1199         break;
1200       case java_thread:
1201         thrtyp = (char *)"java";
1202         break;
1203       case compiler_thread:
1204         thrtyp = (char *)"compiler";
1205         break;
1206       case watcher_thread:
1207         thrtyp = (char *)"watcher";
1208         break;
1209       default:
1210         thrtyp = (char *)"unknown";
1211         break;
1212     }
1213     tty->print_cr("In create_thread, creating a %s thread\n", thrtyp);
1214   }
1215 
1216   // Calculate stack size if it's not specified by caller.
1217   if (stack_size == 0) {
1218     // The default stack size 1M (2M for LP64).
1219     stack_size = (BytesPerWord >> 2) * K * K;
1220 
1221     switch (thr_type) {
1222     case os::java_thread:
1223       // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
1224       if (JavaThread::stack_size_at_create() > 0) stack_size = JavaThread::stack_size_at_create();
1225       break;
1226     case os::compiler_thread:
1227       if (CompilerThreadStackSize > 0) {
1228         stack_size = (size_t)(CompilerThreadStackSize * K);
1229         break;
1230       } // else fall through:
1231         // use VMThreadStackSize if CompilerThreadStackSize is not defined
1232     case os::vm_thread:
1233     case os::pgc_thread:
1234     case os::cgc_thread:
1235     case os::watcher_thread:
1236       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
1237       break;
1238     }
1239   }
1240   stack_size = MAX2(stack_size, os::Solaris::min_stack_allowed);
1241 
1242   // Initial state is ALLOCATED but not INITIALIZED
1243   osthread->set_state(ALLOCATED);
1244 
1245   if (os::Solaris::_os_thread_count > os::Solaris::_os_thread_limit) {
1246     // We got lots of threads. Check if we still have some address space left.
1247     // Need to be at least 5Mb of unreserved address space. We do check by
1248     // trying to reserve some.
1249     const size_t VirtualMemoryBangSize = 20*K*K;
1250     char* mem = os::reserve_memory(VirtualMemoryBangSize);
1251     if (mem == NULL) {
1252       delete osthread;
1253       return false;
1254     } else {
1255       // Release the memory again
1256       os::release_memory(mem, VirtualMemoryBangSize);
1257     }
1258   }
1259 
1260   // Setup osthread because the child thread may need it.
1261   thread->set_osthread(osthread);
1262 
1263   // Create the Solaris thread
1264   // explicit THR_BOUND for T2_libthread case in case
1265   // that assumption is not accurate, but our alternate signal stack
1266   // handling is based on it which must have bound threads
1267   thread_t tid = 0;
1268   long     flags = (UseDetachedThreads ? THR_DETACHED : 0) | THR_SUSPENDED
1269                    | ((UseBoundThreads || os::Solaris::T2_libthread() ||
1270                        (thr_type == vm_thread) ||
1271                        (thr_type == cgc_thread) ||
1272                        (thr_type == pgc_thread) ||
1273                        (thr_type == compiler_thread && BackgroundCompilation)) ?
1274                       THR_BOUND : 0);
1275   int      status;
1276 
1277   // 4376845 -- libthread/kernel don't provide enough LWPs to utilize all CPUs.
1278   //
1279   // On multiprocessors systems, libthread sometimes under-provisions our
1280   // process with LWPs.  On a 30-way systems, for instance, we could have
1281   // 50 user-level threads in ready state and only 2 or 3 LWPs assigned
1282   // to our process.  This can result in under utilization of PEs.
1283   // I suspect the problem is related to libthread's LWP
1284   // pool management and to the kernel's SIGBLOCKING "last LWP parked"
1285   // upcall policy.
1286   //
1287   // The following code is palliative -- it attempts to ensure that our
1288   // process has sufficient LWPs to take advantage of multiple PEs.
1289   // Proper long-term cures include using user-level threads bound to LWPs
1290   // (THR_BOUND) or using LWP-based synchronization.  Note that there is a
1291   // slight timing window with respect to sampling _os_thread_count, but
1292   // the race is benign.  Also, we should periodically recompute
1293   // _processors_online as the min of SC_NPROCESSORS_ONLN and the
1294   // the number of PEs in our partition.  You might be tempted to use
1295   // THR_NEW_LWP here, but I'd recommend against it as that could
1296   // result in undesirable growth of the libthread's LWP pool.
1297   // The fix below isn't sufficient; for instance, it doesn't take into count
1298   // LWPs parked on IO.  It does, however, help certain CPU-bound benchmarks.
1299   //
1300   // Some pathologies this scheme doesn't handle:
1301   // *  Threads can block, releasing the LWPs.  The LWPs can age out.
1302   //    When a large number of threads become ready again there aren't
1303   //    enough LWPs available to service them.  This can occur when the
1304   //    number of ready threads oscillates.
1305   // *  LWPs/Threads park on IO, thus taking the LWP out of circulation.
1306   //
1307   // Finally, we should call thr_setconcurrency() periodically to refresh
1308   // the LWP pool and thwart the LWP age-out mechanism.
1309   // The "+3" term provides a little slop -- we want to slightly overprovision.
1310 
1311   if (AdjustConcurrency && os::Solaris::_os_thread_count < (_processors_online+3)) {
1312     if (!(flags & THR_BOUND)) {
1313       thr_setconcurrency (os::Solaris::_os_thread_count);       // avoid starvation
1314     }
1315   }
1316   // Although this doesn't hurt, we should warn of undefined behavior
1317   // when using unbound T1 threads with schedctl().  This should never
1318   // happen, as the compiler and VM threads are always created bound
1319   DEBUG_ONLY(
1320       if ((VMThreadHintNoPreempt || CompilerThreadHintNoPreempt) &&
1321           (!os::Solaris::T2_libthread() && (!(flags & THR_BOUND))) &&
1322           ((thr_type == vm_thread) || (thr_type == cgc_thread) ||
1323            (thr_type == pgc_thread) || (thr_type == compiler_thread && BackgroundCompilation))) {
1324          warning("schedctl behavior undefined when Compiler/VM/GC Threads are Unbound");
1325       }
1326   );
1327 
1328 
1329   // Mark that we don't have an lwp or thread id yet.
1330   // In case we attempt to set the priority before the thread starts.
1331   osthread->set_lwp_id(-1);
1332   osthread->set_thread_id(-1);
1333 
1334   status = thr_create(NULL, stack_size, java_start, thread, flags, &tid);
1335   if (status != 0) {
1336     if (PrintMiscellaneous && (Verbose || WizardMode)) {
1337       perror("os::create_thread");
1338     }
1339     thread->set_osthread(NULL);
1340     // Need to clean up stuff we've allocated so far
1341     delete osthread;
1342     return false;
1343   }
1344 
1345   Atomic::inc(&os::Solaris::_os_thread_count);
1346 
1347   // Store info on the Solaris thread into the OSThread
1348   osthread->set_thread_id(tid);
1349 
1350   // Remember that we created this thread so we can set priority on it
1351   osthread->set_vm_created();
1352 
1353   // Set the default thread priority.  If using bound threads, setting
1354   // lwp priority will be delayed until thread start.
1355   set_native_priority(thread,
1356                       DefaultThreadPriority == -1 ?
1357                         java_to_os_priority[NormPriority] :
1358                         DefaultThreadPriority);
1359 
1360   // Initial thread state is INITIALIZED, not SUSPENDED
1361   osthread->set_state(INITIALIZED);
1362 
1363   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
1364   return true;
1365 }
1366 
1367 /* defined for >= Solaris 10. This allows builds on earlier versions
1368  *  of Solaris to take advantage of the newly reserved Solaris JVM signals
1369  *  With SIGJVM1, SIGJVM2, INTERRUPT_SIGNAL is SIGJVM1, ASYNC_SIGNAL is SIGJVM2
1370  *  and -XX:+UseAltSigs does nothing since these should have no conflict
1371  */
1372 #if !defined(SIGJVM1)
1373 #define SIGJVM1 39
1374 #define SIGJVM2 40
1375 #endif
1376 
1377 debug_only(static bool signal_sets_initialized = false);
1378 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
1379 int os::Solaris::_SIGinterrupt = INTERRUPT_SIGNAL;
1380 int os::Solaris::_SIGasync = ASYNC_SIGNAL;
1381 
1382 bool os::Solaris::is_sig_ignored(int sig) {
1383       struct sigaction oact;
1384       sigaction(sig, (struct sigaction*)NULL, &oact);
1385       void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oact.sa_sigaction)
1386                                      : CAST_FROM_FN_PTR(void*,  oact.sa_handler);
1387       if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN))
1388            return true;
1389       else
1390            return false;
1391 }
1392 
1393 // Note: SIGRTMIN is a macro that calls sysconf() so it will
1394 // dynamically detect SIGRTMIN value for the system at runtime, not buildtime
1395 static bool isJVM1available() {
1396   return SIGJVM1 < SIGRTMIN;
1397 }
1398 
1399 void os::Solaris::signal_sets_init() {
1400   // Should also have an assertion stating we are still single-threaded.
1401   assert(!signal_sets_initialized, "Already initialized");
1402   // Fill in signals that are necessarily unblocked for all threads in
1403   // the VM. Currently, we unblock the following signals:
1404   // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden
1405   //                         by -Xrs (=ReduceSignalUsage));
1406   // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all
1407   // other threads. The "ReduceSignalUsage" boolean tells us not to alter
1408   // the dispositions or masks wrt these signals.
1409   // Programs embedding the VM that want to use the above signals for their
1410   // own purposes must, at this time, use the "-Xrs" option to prevent
1411   // interference with shutdown hooks and BREAK_SIGNAL thread dumping.
1412   // (See bug 4345157, and other related bugs).
1413   // In reality, though, unblocking these signals is really a nop, since
1414   // these signals are not blocked by default.
1415   sigemptyset(&unblocked_sigs);
1416   sigemptyset(&allowdebug_blocked_sigs);
1417   sigaddset(&unblocked_sigs, SIGILL);
1418   sigaddset(&unblocked_sigs, SIGSEGV);
1419   sigaddset(&unblocked_sigs, SIGBUS);
1420   sigaddset(&unblocked_sigs, SIGFPE);
1421 
1422   if (isJVM1available) {
1423     os::Solaris::set_SIGinterrupt(SIGJVM1);
1424     os::Solaris::set_SIGasync(SIGJVM2);
1425   } else if (UseAltSigs) {
1426     os::Solaris::set_SIGinterrupt(ALT_INTERRUPT_SIGNAL);
1427     os::Solaris::set_SIGasync(ALT_ASYNC_SIGNAL);
1428   } else {
1429     os::Solaris::set_SIGinterrupt(INTERRUPT_SIGNAL);
1430     os::Solaris::set_SIGasync(ASYNC_SIGNAL);
1431   }
1432 
1433   sigaddset(&unblocked_sigs, os::Solaris::SIGinterrupt());
1434   sigaddset(&unblocked_sigs, os::Solaris::SIGasync());
1435 
1436   if (!ReduceSignalUsage) {
1437    if (!os::Solaris::is_sig_ignored(SHUTDOWN1_SIGNAL)) {
1438       sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);
1439       sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL);
1440    }
1441    if (!os::Solaris::is_sig_ignored(SHUTDOWN2_SIGNAL)) {
1442       sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);
1443       sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL);
1444    }
1445    if (!os::Solaris::is_sig_ignored(SHUTDOWN3_SIGNAL)) {
1446       sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);
1447       sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL);
1448    }
1449   }
1450   // Fill in signals that are blocked by all but the VM thread.
1451   sigemptyset(&vm_sigs);
1452   if (!ReduceSignalUsage)
1453     sigaddset(&vm_sigs, BREAK_SIGNAL);
1454   debug_only(signal_sets_initialized = true);
1455 
1456   // For diagnostics only used in run_periodic_checks
1457   sigemptyset(&check_signal_done);
1458 }
1459 
1460 // These are signals that are unblocked while a thread is running Java.
1461 // (For some reason, they get blocked by default.)
1462 sigset_t* os::Solaris::unblocked_signals() {
1463   assert(signal_sets_initialized, "Not initialized");
1464   return &unblocked_sigs;
1465 }
1466 
1467 // These are the signals that are blocked while a (non-VM) thread is
1468 // running Java. Only the VM thread handles these signals.
1469 sigset_t* os::Solaris::vm_signals() {
1470   assert(signal_sets_initialized, "Not initialized");
1471   return &vm_sigs;
1472 }
1473 
1474 // These are signals that are blocked during cond_wait to allow debugger in
1475 sigset_t* os::Solaris::allowdebug_blocked_signals() {
1476   assert(signal_sets_initialized, "Not initialized");
1477   return &allowdebug_blocked_sigs;
1478 }
1479 
1480 
1481 void _handle_uncaught_cxx_exception() {
1482   VMError err("An uncaught C++ exception");
1483   err.report_and_die();
1484 }
1485 
1486 
1487 // First crack at OS-specific initialization, from inside the new thread.
1488 void os::initialize_thread(Thread* thr) {
1489   int r = thr_main() ;
1490   guarantee (r == 0 || r == 1, "CR6501650 or CR6493689") ;
1491   if (r) {
1492     JavaThread* jt = (JavaThread *)thr;
1493     assert(jt != NULL,"Sanity check");
1494     size_t stack_size;
1495     address base = jt->stack_base();
1496     if (Arguments::created_by_java_launcher()) {
1497       // Use 2MB to allow for Solaris 7 64 bit mode.
1498       stack_size = JavaThread::stack_size_at_create() == 0
1499         ? 2048*K : JavaThread::stack_size_at_create();
1500 
1501       // There are rare cases when we may have already used more than
1502       // the basic stack size allotment before this method is invoked.
1503       // Attempt to allow for a normally sized java_stack.
1504       size_t current_stack_offset = (size_t)(base - (address)&stack_size);
1505       stack_size += ReservedSpace::page_align_size_down(current_stack_offset);
1506     } else {
1507       // 6269555: If we were not created by a Java launcher, i.e. if we are
1508       // running embedded in a native application, treat the primordial thread
1509       // as much like a native attached thread as possible.  This means using
1510       // the current stack size from thr_stksegment(), unless it is too large
1511       // to reliably setup guard pages.  A reasonable max size is 8MB.
1512       size_t current_size = current_stack_size();
1513       // This should never happen, but just in case....
1514       if (current_size == 0) current_size = 2 * K * K;
1515       stack_size = current_size > (8 * K * K) ? (8 * K * K) : current_size;
1516     }
1517     address bottom = (address)align_size_up((intptr_t)(base - stack_size), os::vm_page_size());;
1518     stack_size = (size_t)(base - bottom);
1519 
1520     assert(stack_size > 0, "Stack size calculation problem");
1521 
1522     if (stack_size > jt->stack_size()) {
1523       NOT_PRODUCT(
1524         struct rlimit limits;
1525         getrlimit(RLIMIT_STACK, &limits);
1526         size_t size = adjust_stack_size(base, (size_t)limits.rlim_cur);
1527         assert(size >= jt->stack_size(), "Stack size problem in main thread");
1528       )
1529       tty->print_cr(
1530         "Stack size of %d Kb exceeds current limit of %d Kb.\n"
1531         "(Stack sizes are rounded up to a multiple of the system page size.)\n"
1532         "See limit(1) to increase the stack size limit.",
1533         stack_size / K, jt->stack_size() / K);
1534       vm_exit(1);
1535     }
1536     assert(jt->stack_size() >= stack_size,
1537           "Attempt to map more stack than was allocated");
1538     jt->set_stack_size(stack_size);
1539   }
1540 
1541    // 5/22/01: Right now alternate signal stacks do not handle
1542    // throwing stack overflow exceptions, see bug 4463178
1543    // Until a fix is found for this, T2 will NOT imply alternate signal
1544    // stacks.
1545    // If using T2 libthread threads, install an alternate signal stack.
1546    // Because alternate stacks associate with LWPs on Solaris,
1547    // see sigaltstack(2), if using UNBOUND threads, or if UseBoundThreads
1548    // we prefer to explicitly stack bang.
1549    // If not using T2 libthread, but using UseBoundThreads any threads
1550    // (primordial thread, jni_attachCurrentThread) we do not create,
1551    // probably are not bound, therefore they can not have an alternate
1552    // signal stack. Since our stack banging code is generated and
1553    // is shared across threads, all threads must be bound to allow
1554    // using alternate signal stacks.  The alternative is to interpose
1555    // on _lwp_create to associate an alt sig stack with each LWP,
1556    // and this could be a problem when the JVM is embedded.
1557    // We would prefer to use alternate signal stacks with T2
1558    // Since there is currently no accurate way to detect T2
1559    // we do not. Assuming T2 when running T1 causes sig 11s or assertions
1560    // on installing alternate signal stacks
1561 
1562 
1563    // 05/09/03: removed alternate signal stack support for Solaris
1564    // The alternate signal stack mechanism is no longer needed to
1565    // handle stack overflow. This is now handled by allocating
1566    // guard pages (red zone) and stackbanging.
1567    // Initially the alternate signal stack mechanism was removed because
1568    // it did not work with T1 llibthread. Alternate
1569    // signal stacks MUST have all threads bound to lwps. Applications
1570    // can create their own threads and attach them without their being
1571    // bound under T1. This is frequently the case for the primordial thread.
1572    // If we were ever to reenable this mechanism we would need to
1573    // use the dynamic check for T2 libthread.
1574 
1575   os::Solaris::init_thread_fpu_state();
1576   std::set_terminate(_handle_uncaught_cxx_exception);
1577 }
1578 
1579 
1580 
1581 // Free Solaris resources related to the OSThread
1582 void os::free_thread(OSThread* osthread) {
1583   assert(osthread != NULL, "os::free_thread but osthread not set");
1584 
1585 
1586   // We are told to free resources of the argument thread,
1587   // but we can only really operate on the current thread.
1588   // The main thread must take the VMThread down synchronously
1589   // before the main thread exits and frees up CodeHeap
1590   guarantee((Thread::current()->osthread() == osthread
1591      || (osthread == VMThread::vm_thread()->osthread())), "os::free_thread but not current thread");
1592   if (Thread::current()->osthread() == osthread) {
1593     // Restore caller's signal mask
1594     sigset_t sigmask = osthread->caller_sigmask();
1595     thr_sigsetmask(SIG_SETMASK, &sigmask, NULL);
1596   }
1597   delete osthread;
1598 }
1599 
1600 void os::pd_start_thread(Thread* thread) {
1601   int status = thr_continue(thread->osthread()->thread_id());
1602   assert_status(status == 0, status, "thr_continue failed");
1603 }
1604 
1605 
1606 intx os::current_thread_id() {
1607   return (intx)thr_self();
1608 }
1609 
1610 static pid_t _initial_pid = 0;
1611 
1612 int os::current_process_id() {
1613   return (int)(_initial_pid ? _initial_pid : getpid());
1614 }
1615 
1616 int os::allocate_thread_local_storage() {
1617   // %%%       in Win32 this allocates a memory segment pointed to by a
1618   //           register.  Dan Stein can implement a similar feature in
1619   //           Solaris.  Alternatively, the VM can do the same thing
1620   //           explicitly: malloc some storage and keep the pointer in a
1621   //           register (which is part of the thread's context) (or keep it
1622   //           in TLS).
1623   // %%%       In current versions of Solaris, thr_self and TSD can
1624   //           be accessed via short sequences of displaced indirections.
1625   //           The value of thr_self is available as %g7(36).
1626   //           The value of thr_getspecific(k) is stored in %g7(12)(4)(k*4-4),
1627   //           assuming that the current thread already has a value bound to k.
1628   //           It may be worth experimenting with such access patterns,
1629   //           and later having the parameters formally exported from a Solaris
1630   //           interface.  I think, however, that it will be faster to
1631   //           maintain the invariant that %g2 always contains the
1632   //           JavaThread in Java code, and have stubs simply
1633   //           treat %g2 as a caller-save register, preserving it in a %lN.
1634   thread_key_t tk;
1635   if (thr_keycreate( &tk, NULL ) )
1636     fatal(err_msg("os::allocate_thread_local_storage: thr_keycreate failed "
1637                   "(%s)", strerror(errno)));
1638   return int(tk);
1639 }
1640 
1641 void os::free_thread_local_storage(int index) {
1642   // %%% don't think we need anything here
1643   // if ( pthread_key_delete((pthread_key_t) tk) )
1644   //   fatal("os::free_thread_local_storage: pthread_key_delete failed");
1645 }
1646 
1647 #define SMALLINT 32   // libthread allocate for tsd_common is a version specific
1648                       // small number - point is NO swap space available
1649 void os::thread_local_storage_at_put(int index, void* value) {
1650   // %%% this is used only in threadLocalStorage.cpp
1651   if (thr_setspecific((thread_key_t)index, value)) {
1652     if (errno == ENOMEM) {
1653        vm_exit_out_of_memory(SMALLINT, "thr_setspecific: out of swap space");
1654     } else {
1655       fatal(err_msg("os::thread_local_storage_at_put: thr_setspecific failed "
1656                     "(%s)", strerror(errno)));
1657     }
1658   } else {
1659       ThreadLocalStorage::set_thread_in_slot ((Thread *) value) ;
1660   }
1661 }
1662 
1663 // This function could be called before TLS is initialized, for example, when
1664 // VM receives an async signal or when VM causes a fatal error during
1665 // initialization. Return NULL if thr_getspecific() fails.
1666 void* os::thread_local_storage_at(int index) {
1667   // %%% this is used only in threadLocalStorage.cpp
1668   void* r = NULL;
1669   return thr_getspecific((thread_key_t)index, &r) != 0 ? NULL : r;
1670 }
1671 
1672 
1673 // gethrtime can move backwards if read from one cpu and then a different cpu
1674 // getTimeNanos is guaranteed to not move backward on Solaris
1675 // local spinloop created as faster for a CAS on an int than
1676 // a CAS on a 64bit jlong. Also Atomic::cmpxchg for jlong is not
1677 // supported on sparc v8 or pre supports_cx8 intel boxes.
1678 // oldgetTimeNanos for systems which do not support CAS on 64bit jlong
1679 // i.e. sparc v8 and pre supports_cx8 (i486) intel boxes
1680 inline hrtime_t oldgetTimeNanos() {
1681   int gotlock = LOCK_INVALID;
1682   hrtime_t newtime = gethrtime();
1683 
1684   for (;;) {
1685 // grab lock for max_hrtime
1686     int curlock = max_hrtime_lock;
1687     if (curlock & LOCK_BUSY)  continue;
1688     if (gotlock = Atomic::cmpxchg(LOCK_BUSY, &max_hrtime_lock, LOCK_FREE) != LOCK_FREE) continue;
1689     if (newtime > max_hrtime) {
1690       max_hrtime = newtime;
1691     } else {
1692       newtime = max_hrtime;
1693     }
1694     // release lock
1695     max_hrtime_lock = LOCK_FREE;
1696     return newtime;
1697   }
1698 }
1699 // gethrtime can move backwards if read from one cpu and then a different cpu
1700 // getTimeNanos is guaranteed to not move backward on Solaris
1701 inline hrtime_t getTimeNanos() {
1702   if (VM_Version::supports_cx8()) {
1703     const hrtime_t now = gethrtime();
1704     // Use atomic long load since 32-bit x86 uses 2 registers to keep long.
1705     const hrtime_t prev = Atomic::load((volatile jlong*)&max_hrtime);
1706     if (now <= prev)  return prev;   // same or retrograde time;
1707     const hrtime_t obsv = Atomic::cmpxchg(now, (volatile jlong*)&max_hrtime, prev);
1708     assert(obsv >= prev, "invariant");   // Monotonicity
1709     // If the CAS succeeded then we're done and return "now".
1710     // If the CAS failed and the observed value "obs" is >= now then
1711     // we should return "obs".  If the CAS failed and now > obs > prv then
1712     // some other thread raced this thread and installed a new value, in which case
1713     // we could either (a) retry the entire operation, (b) retry trying to install now
1714     // or (c) just return obs.  We use (c).   No loop is required although in some cases
1715     // we might discard a higher "now" value in deference to a slightly lower but freshly
1716     // installed obs value.   That's entirely benign -- it admits no new orderings compared
1717     // to (a) or (b) -- and greatly reduces coherence traffic.
1718     // We might also condition (c) on the magnitude of the delta between obs and now.
1719     // Avoiding excessive CAS operations to hot RW locations is critical.
1720     // See http://blogs.sun.com/dave/entry/cas_and_cache_trivia_invalidate
1721     return (prev == obsv) ? now : obsv ;
1722   } else {
1723     return oldgetTimeNanos();
1724   }
1725 }
1726 
1727 // Time since start-up in seconds to a fine granularity.
1728 // Used by VMSelfDestructTimer and the MemProfiler.
1729 double os::elapsedTime() {
1730   return (double)(getTimeNanos() - first_hrtime) / (double)hrtime_hz;
1731 }
1732 
1733 jlong os::elapsed_counter() {
1734   return (jlong)(getTimeNanos() - first_hrtime);
1735 }
1736 
1737 jlong os::elapsed_frequency() {
1738    return hrtime_hz;
1739 }
1740 
1741 // Return the real, user, and system times in seconds from an
1742 // arbitrary fixed point in the past.
1743 bool os::getTimesSecs(double* process_real_time,
1744                   double* process_user_time,
1745                   double* process_system_time) {
1746   struct tms ticks;
1747   clock_t real_ticks = times(&ticks);
1748 
1749   if (real_ticks == (clock_t) (-1)) {
1750     return false;
1751   } else {
1752     double ticks_per_second = (double) clock_tics_per_sec;
1753     *process_user_time = ((double) ticks.tms_utime) / ticks_per_second;
1754     *process_system_time = ((double) ticks.tms_stime) / ticks_per_second;
1755     // For consistency return the real time from getTimeNanos()
1756     // converted to seconds.
1757     *process_real_time = ((double) getTimeNanos()) / ((double) NANOUNITS);
1758 
1759     return true;
1760   }
1761 }
1762 
1763 bool os::supports_vtime() { return true; }
1764 
1765 bool os::enable_vtime() {
1766   int fd = ::open("/proc/self/ctl", O_WRONLY);
1767   if (fd == -1)
1768     return false;
1769 
1770   long cmd[] = { PCSET, PR_MSACCT };
1771   int res = ::write(fd, cmd, sizeof(long) * 2);
1772   ::close(fd);
1773   if (res != sizeof(long) * 2)
1774     return false;
1775 
1776   return true;
1777 }
1778 
1779 bool os::vtime_enabled() {
1780   int fd = ::open("/proc/self/status", O_RDONLY);
1781   if (fd == -1)
1782     return false;
1783 
1784   pstatus_t status;
1785   int res = os::read(fd, (void*) &status, sizeof(pstatus_t));
1786   ::close(fd);
1787   if (res != sizeof(pstatus_t))
1788     return false;
1789 
1790   return status.pr_flags & PR_MSACCT;
1791 }
1792 
1793 double os::elapsedVTime() {
1794   return (double)gethrvtime() / (double)hrtime_hz;
1795 }
1796 
1797 // Used internally for comparisons only
1798 // getTimeMillis guaranteed to not move backwards on Solaris
1799 jlong getTimeMillis() {
1800   jlong nanotime = getTimeNanos();
1801   return (jlong)(nanotime / NANOSECS_PER_MILLISEC);
1802 }
1803 
1804 // Must return millis since Jan 1 1970 for JVM_CurrentTimeMillis
1805 jlong os::javaTimeMillis() {
1806   timeval t;
1807   if (gettimeofday( &t, NULL) == -1)
1808     fatal(err_msg("os::javaTimeMillis: gettimeofday (%s)", strerror(errno)));
1809   return jlong(t.tv_sec) * 1000  +  jlong(t.tv_usec) / 1000;
1810 }
1811 
1812 jlong os::javaTimeNanos() {
1813   return (jlong)getTimeNanos();
1814 }
1815 
1816 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
1817   info_ptr->max_value = ALL_64_BITS;      // gethrtime() uses all 64 bits
1818   info_ptr->may_skip_backward = false;    // not subject to resetting or drifting
1819   info_ptr->may_skip_forward = false;     // not subject to resetting or drifting
1820   info_ptr->kind = JVMTI_TIMER_ELAPSED;   // elapsed not CPU time
1821 }
1822 
1823 char * os::local_time_string(char *buf, size_t buflen) {
1824   struct tm t;
1825   time_t long_time;
1826   time(&long_time);
1827   localtime_r(&long_time, &t);
1828   jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
1829                t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
1830                t.tm_hour, t.tm_min, t.tm_sec);
1831   return buf;
1832 }
1833 
1834 // Note: os::shutdown() might be called very early during initialization, or
1835 // called from signal handler. Before adding something to os::shutdown(), make
1836 // sure it is async-safe and can handle partially initialized VM.
1837 void os::shutdown() {
1838 
1839   // allow PerfMemory to attempt cleanup of any persistent resources
1840   perfMemory_exit();
1841 
1842   // needs to remove object in file system
1843   AttachListener::abort();
1844 
1845   // flush buffered output, finish log files
1846   ostream_abort();
1847 
1848   // Check for abort hook
1849   abort_hook_t abort_hook = Arguments::abort_hook();
1850   if (abort_hook != NULL) {
1851     abort_hook();
1852   }
1853 }
1854 
1855 // Note: os::abort() might be called very early during initialization, or
1856 // called from signal handler. Before adding something to os::abort(), make
1857 // sure it is async-safe and can handle partially initialized VM.
1858 void os::abort(bool dump_core) {
1859   os::shutdown();
1860   if (dump_core) {
1861 #ifndef PRODUCT
1862     fdStream out(defaultStream::output_fd());
1863     out.print_raw("Current thread is ");
1864     char buf[16];
1865     jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
1866     out.print_raw_cr(buf);
1867     out.print_raw_cr("Dumping core ...");
1868 #endif
1869     ::abort(); // dump core (for debugging)
1870   }
1871 
1872   ::exit(1);
1873 }
1874 
1875 // Die immediately, no exit hook, no abort hook, no cleanup.
1876 void os::die() {
1877   ::abort(); // dump core (for debugging)
1878 }
1879 
1880 // unused
1881 void os::set_error_file(const char *logfile) {}
1882 
1883 // DLL functions
1884 
1885 const char* os::dll_file_extension() { return ".so"; }
1886 
1887 // This must be hard coded because it's the system's temporary
1888 // directory not the java application's temp directory, ala java.io.tmpdir.
1889 const char* os::get_temp_directory() { return "/tmp"; }
1890 
1891 static bool file_exists(const char* filename) {
1892   struct stat statbuf;
1893   if (filename == NULL || strlen(filename) == 0) {
1894     return false;
1895   }
1896   return os::stat(filename, &statbuf) == 0;
1897 }
1898 
1899 void os::dll_build_name(char* buffer, size_t buflen,
1900                         const char* pname, const char* fname) {
1901   const size_t pnamelen = pname ? strlen(pname) : 0;
1902 
1903   // Quietly truncate on buffer overflow.  Should be an error.
1904   if (pnamelen + strlen(fname) + 10 > (size_t) buflen) {
1905     *buffer = '\0';
1906     return;
1907   }
1908 
1909   if (pnamelen == 0) {
1910     snprintf(buffer, buflen, "lib%s.so", fname);
1911   } else if (strchr(pname, *os::path_separator()) != NULL) {
1912     int n;
1913     char** pelements = split_path(pname, &n);
1914     for (int i = 0 ; i < n ; i++) {
1915       // really shouldn't be NULL but what the heck, check can't hurt
1916       if (pelements[i] == NULL || strlen(pelements[i]) == 0) {
1917         continue; // skip the empty path values
1918       }
1919       snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname);
1920       if (file_exists(buffer)) {
1921         break;
1922       }
1923     }
1924     // release the storage
1925     for (int i = 0 ; i < n ; i++) {
1926       if (pelements[i] != NULL) {
1927         FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);
1928       }
1929     }
1930     if (pelements != NULL) {
1931       FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);
1932     }
1933   } else {
1934     snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);
1935   }
1936 }
1937 
1938 const char* os::get_current_directory(char *buf, int buflen) {
1939   return getcwd(buf, buflen);
1940 }
1941 
1942 // check if addr is inside libjvm[_g].so
1943 bool os::address_is_in_vm(address addr) {
1944   static address libjvm_base_addr;
1945   Dl_info dlinfo;
1946 
1947   if (libjvm_base_addr == NULL) {
1948     dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo);
1949     libjvm_base_addr = (address)dlinfo.dli_fbase;
1950     assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
1951   }
1952 
1953   if (dladdr((void *)addr, &dlinfo)) {
1954     if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1955   }
1956 
1957   return false;
1958 }
1959 
1960 typedef int (*dladdr1_func_type) (void *, Dl_info *, void **, int);
1961 static dladdr1_func_type dladdr1_func = NULL;
1962 
1963 bool os::dll_address_to_function_name(address addr, char *buf,
1964                                       int buflen, int * offset) {
1965   Dl_info dlinfo;
1966 
1967   // dladdr1_func was initialized in os::init()
1968   if (dladdr1_func){
1969       // yes, we have dladdr1
1970 
1971       // Support for dladdr1 is checked at runtime; it may be
1972       // available even if the vm is built on a machine that does
1973       // not have dladdr1 support.  Make sure there is a value for
1974       // RTLD_DL_SYMENT.
1975       #ifndef RTLD_DL_SYMENT
1976       #define RTLD_DL_SYMENT 1
1977       #endif
1978 #ifdef _LP64
1979       Elf64_Sym * info;
1980 #else
1981       Elf32_Sym * info;
1982 #endif
1983       if (dladdr1_func((void *)addr, &dlinfo, (void **)&info,
1984                        RTLD_DL_SYMENT)) {
1985         if ((char *)dlinfo.dli_saddr + info->st_size > (char *)addr) {
1986           if (buf != NULL) {
1987             if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen))
1988               jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1989             }
1990             if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
1991             return true;
1992         }
1993       }
1994       if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != 0) {
1995         if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1996            buf, buflen, offset, dlinfo.dli_fname)) {
1997           return true;
1998         }
1999       }
2000       if (buf != NULL) buf[0] = '\0';
2001       if (offset != NULL) *offset  = -1;
2002       return false;
2003   } else {
2004       // no, only dladdr is available
2005       if (dladdr((void *)addr, &dlinfo)) {
2006         if (buf != NULL) {
2007           if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen))
2008             jio_snprintf(buf, buflen, dlinfo.dli_sname);
2009         }
2010         if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
2011         return true;
2012       } else if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != 0) {
2013         if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
2014           buf, buflen, offset, dlinfo.dli_fname)) {
2015           return true;
2016         }
2017       }
2018       if (buf != NULL) buf[0] = '\0';
2019       if (offset != NULL) *offset  = -1;
2020       return false;
2021   }
2022 }
2023 
2024 bool os::dll_address_to_library_name(address addr, char* buf,
2025                                      int buflen, int* offset) {
2026   Dl_info dlinfo;
2027 
2028   if (dladdr((void*)addr, &dlinfo)){
2029      if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
2030      if (offset) *offset = addr - (address)dlinfo.dli_fbase;
2031      return true;
2032   } else {
2033      if (buf) buf[0] = '\0';
2034      if (offset) *offset = -1;
2035      return false;
2036   }
2037 }
2038 
2039 // Prints the names and full paths of all opened dynamic libraries
2040 // for current process
2041 void os::print_dll_info(outputStream * st) {
2042     Dl_info dli;
2043     void *handle;
2044     Link_map *map;
2045     Link_map *p;
2046 
2047     st->print_cr("Dynamic libraries:"); st->flush();
2048 
2049     if (!dladdr(CAST_FROM_FN_PTR(void *, os::print_dll_info), &dli)) {
2050         st->print_cr("Error: Cannot print dynamic libraries.");
2051         return;
2052     }
2053     handle = dlopen(dli.dli_fname, RTLD_LAZY);
2054     if (handle == NULL) {
2055         st->print_cr("Error: Cannot print dynamic libraries.");
2056         return;
2057     }
2058     dlinfo(handle, RTLD_DI_LINKMAP, &map);
2059     if (map == NULL) {
2060         st->print_cr("Error: Cannot print dynamic libraries.");
2061         return;
2062     }
2063 
2064     while (map->l_prev != NULL)
2065         map = map->l_prev;
2066 
2067     while (map != NULL) {
2068         st->print_cr(PTR_FORMAT " \t%s", map->l_addr, map->l_name);
2069         map = map->l_next;
2070     }
2071 
2072     dlclose(handle);
2073 }
2074 
2075   // Loads .dll/.so and
2076   // in case of error it checks if .dll/.so was built for the
2077   // same architecture as Hotspot is running on
2078 
2079 void * os::dll_load(const char *filename, char *ebuf, int ebuflen)
2080 {
2081   void * result= ::dlopen(filename, RTLD_LAZY);
2082   if (result != NULL) {
2083     // Successful loading
2084     return result;
2085   }
2086 
2087   Elf32_Ehdr elf_head;
2088 
2089   // Read system error message into ebuf
2090   // It may or may not be overwritten below
2091   ::strncpy(ebuf, ::dlerror(), ebuflen-1);
2092   ebuf[ebuflen-1]='\0';
2093   int diag_msg_max_length=ebuflen-strlen(ebuf);
2094   char* diag_msg_buf=ebuf+strlen(ebuf);
2095 
2096   if (diag_msg_max_length==0) {
2097     // No more space in ebuf for additional diagnostics message
2098     return NULL;
2099   }
2100 
2101 
2102   int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
2103 
2104   if (file_descriptor < 0) {
2105     // Can't open library, report dlerror() message
2106     return NULL;
2107   }
2108 
2109   bool failed_to_read_elf_head=
2110     (sizeof(elf_head)!=
2111         (::read(file_descriptor, &elf_head,sizeof(elf_head)))) ;
2112 
2113   ::close(file_descriptor);
2114   if (failed_to_read_elf_head) {
2115     // file i/o error - report dlerror() msg
2116     return NULL;
2117   }
2118 
2119   typedef struct {
2120     Elf32_Half  code;         // Actual value as defined in elf.h
2121     Elf32_Half  compat_class; // Compatibility of archs at VM's sense
2122     char        elf_class;    // 32 or 64 bit
2123     char        endianess;    // MSB or LSB
2124     char*       name;         // String representation
2125   } arch_t;
2126 
2127   static const arch_t arch_array[]={
2128     {EM_386,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
2129     {EM_486,         EM_386,     ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
2130     {EM_IA_64,       EM_IA_64,   ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
2131     {EM_X86_64,      EM_X86_64,  ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
2132     {EM_SPARC,       EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
2133     {EM_SPARC32PLUS, EM_SPARC,   ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
2134     {EM_SPARCV9,     EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
2135     {EM_PPC,         EM_PPC,     ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
2136     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
2137     {EM_ARM,         EM_ARM,     ELFCLASS32, ELFDATA2LSB, (char*)"ARM 32"}
2138   };
2139 
2140   #if  (defined IA32)
2141     static  Elf32_Half running_arch_code=EM_386;
2142   #elif   (defined AMD64)
2143     static  Elf32_Half running_arch_code=EM_X86_64;
2144   #elif  (defined IA64)
2145     static  Elf32_Half running_arch_code=EM_IA_64;
2146   #elif  (defined __sparc) && (defined _LP64)
2147     static  Elf32_Half running_arch_code=EM_SPARCV9;
2148   #elif  (defined __sparc) && (!defined _LP64)
2149     static  Elf32_Half running_arch_code=EM_SPARC;
2150   #elif  (defined __powerpc64__)
2151     static  Elf32_Half running_arch_code=EM_PPC64;
2152   #elif  (defined __powerpc__)
2153     static  Elf32_Half running_arch_code=EM_PPC;
2154   #elif (defined ARM)
2155     static  Elf32_Half running_arch_code=EM_ARM;
2156   #else
2157     #error Method os::dll_load requires that one of following is defined:\
2158          IA32, AMD64, IA64, __sparc, __powerpc__, ARM, ARM
2159   #endif
2160 
2161   // Identify compatability class for VM's architecture and library's architecture
2162   // Obtain string descriptions for architectures
2163 
2164   arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
2165   int running_arch_index=-1;
2166 
2167   for (unsigned int i=0 ; i < ARRAY_SIZE(arch_array) ; i++ ) {
2168     if (running_arch_code == arch_array[i].code) {
2169       running_arch_index    = i;
2170     }
2171     if (lib_arch.code == arch_array[i].code) {
2172       lib_arch.compat_class = arch_array[i].compat_class;
2173       lib_arch.name         = arch_array[i].name;
2174     }
2175   }
2176 
2177   assert(running_arch_index != -1,
2178     "Didn't find running architecture code (running_arch_code) in arch_array");
2179   if (running_arch_index == -1) {
2180     // Even though running architecture detection failed
2181     // we may still continue with reporting dlerror() message
2182     return NULL;
2183   }
2184 
2185   if (lib_arch.endianess != arch_array[running_arch_index].endianess) {
2186     ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");
2187     return NULL;
2188   }
2189 
2190   if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
2191     ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");
2192     return NULL;
2193   }
2194 
2195   if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
2196     if ( lib_arch.name!=NULL ) {
2197       ::snprintf(diag_msg_buf, diag_msg_max_length-1,
2198         " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
2199         lib_arch.name, arch_array[running_arch_index].name);
2200     } else {
2201       ::snprintf(diag_msg_buf, diag_msg_max_length-1,
2202       " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
2203         lib_arch.code,
2204         arch_array[running_arch_index].name);
2205     }
2206   }
2207 
2208   return NULL;
2209 }
2210 
2211 void* os::dll_lookup(void* handle, const char* name) {
2212   return dlsym(handle, name);
2213 }
2214 
2215 int os::stat(const char *path, struct stat *sbuf) {
2216   char pathbuf[MAX_PATH];
2217   if (strlen(path) > MAX_PATH - 1) {
2218     errno = ENAMETOOLONG;
2219     return -1;
2220   }
2221   os::native_path(strcpy(pathbuf, path));
2222   return ::stat(pathbuf, sbuf);
2223 }
2224 
2225 static bool _print_ascii_file(const char* filename, outputStream* st) {
2226   int fd = ::open(filename, O_RDONLY);
2227   if (fd == -1) {
2228      return false;
2229   }
2230 
2231   char buf[32];
2232   int bytes;
2233   while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
2234     st->print_raw(buf, bytes);
2235   }
2236 
2237   ::close(fd);
2238 
2239   return true;
2240 }
2241 
2242 void os::print_os_info_brief(outputStream* st) {
2243   os::Solaris::print_distro_info(st);
2244 
2245   os::Posix::print_uname_info(st);
2246 
2247   os::Solaris::print_libversion_info(st);
2248 }
2249 
2250 void os::print_os_info(outputStream* st) {
2251   st->print("OS:");
2252 
2253   os::Solaris::print_distro_info(st);
2254 
2255   os::Posix::print_uname_info(st);
2256 
2257   os::Solaris::print_libversion_info(st);
2258 
2259   os::Posix::print_rlimit_info(st);
2260 
2261   os::Posix::print_load_average(st);
2262 }
2263 
2264 void os::Solaris::print_distro_info(outputStream* st) {
2265   if (!_print_ascii_file("/etc/release", st)) {
2266       st->print("Solaris");
2267     }
2268     st->cr();
2269 }
2270 
2271 void os::Solaris::print_libversion_info(outputStream* st) {
2272   if (os::Solaris::T2_libthread()) {
2273     st->print("  (T2 libthread)");
2274   }
2275   else {
2276     st->print("  (T1 libthread)");
2277   }
2278   st->cr();
2279 }
2280 
2281 static bool check_addr0(outputStream* st) {
2282   jboolean status = false;
2283   int fd = ::open("/proc/self/map",O_RDONLY);
2284   if (fd >= 0) {
2285     prmap_t p;
2286     while(::read(fd, &p, sizeof(p)) > 0) {
2287       if (p.pr_vaddr == 0x0) {
2288         st->print("Warning: Address: 0x%x, Size: %dK, ",p.pr_vaddr, p.pr_size/1024, p.pr_mapname);
2289         st->print("Mapped file: %s, ", p.pr_mapname[0] == '\0' ? "None" : p.pr_mapname);
2290         st->print("Access:");
2291         st->print("%s",(p.pr_mflags & MA_READ)  ? "r" : "-");
2292         st->print("%s",(p.pr_mflags & MA_WRITE) ? "w" : "-");
2293         st->print("%s",(p.pr_mflags & MA_EXEC)  ? "x" : "-");
2294         st->cr();
2295         status = true;
2296       }
2297       ::close(fd);
2298     }
2299   }
2300   return status;
2301 }
2302 
2303 void os::pd_print_cpu_info(outputStream* st) {
2304   // Nothing to do for now.
2305 }
2306 
2307 void os::print_memory_info(outputStream* st) {
2308   st->print("Memory:");
2309   st->print(" %dk page", os::vm_page_size()>>10);
2310   st->print(", physical " UINT64_FORMAT "k", os::physical_memory()>>10);
2311   st->print("(" UINT64_FORMAT "k free)", os::available_memory() >> 10);
2312   st->cr();
2313   (void) check_addr0(st);
2314 }
2315 
2316 // Taken from /usr/include/sys/machsig.h  Supposed to be architecture specific
2317 // but they're the same for all the solaris architectures that we support.
2318 const char *ill_names[] = { "ILL0", "ILL_ILLOPC", "ILL_ILLOPN", "ILL_ILLADR",
2319                           "ILL_ILLTRP", "ILL_PRVOPC", "ILL_PRVREG",
2320                           "ILL_COPROC", "ILL_BADSTK" };
2321 
2322 const char *fpe_names[] = { "FPE0", "FPE_INTDIV", "FPE_INTOVF", "FPE_FLTDIV",
2323                           "FPE_FLTOVF", "FPE_FLTUND", "FPE_FLTRES",
2324                           "FPE_FLTINV", "FPE_FLTSUB" };
2325 
2326 const char *segv_names[] = { "SEGV0", "SEGV_MAPERR", "SEGV_ACCERR" };
2327 
2328 const char *bus_names[] = { "BUS0", "BUS_ADRALN", "BUS_ADRERR", "BUS_OBJERR" };
2329 
2330 void os::print_siginfo(outputStream* st, void* siginfo) {
2331   st->print("siginfo:");
2332 
2333   const int buflen = 100;
2334   char buf[buflen];
2335   siginfo_t *si = (siginfo_t*)siginfo;
2336   st->print("si_signo=%s: ", os::exception_name(si->si_signo, buf, buflen));
2337   char *err = strerror(si->si_errno);
2338   if (si->si_errno != 0 && err != NULL) {
2339     st->print("si_errno=%s", err);
2340   } else {
2341     st->print("si_errno=%d", si->si_errno);
2342   }
2343   const int c = si->si_code;
2344   assert(c > 0, "unexpected si_code");
2345   switch (si->si_signo) {
2346   case SIGILL:
2347     st->print(", si_code=%d (%s)", c, c > 8 ? "" : ill_names[c]);
2348     st->print(", si_addr=" PTR_FORMAT, si->si_addr);
2349     break;
2350   case SIGFPE:
2351     st->print(", si_code=%d (%s)", c, c > 9 ? "" : fpe_names[c]);
2352     st->print(", si_addr=" PTR_FORMAT, si->si_addr);
2353     break;
2354   case SIGSEGV:
2355     st->print(", si_code=%d (%s)", c, c > 2 ? "" : segv_names[c]);
2356     st->print(", si_addr=" PTR_FORMAT, si->si_addr);
2357     break;
2358   case SIGBUS:
2359     st->print(", si_code=%d (%s)", c, c > 3 ? "" : bus_names[c]);
2360     st->print(", si_addr=" PTR_FORMAT, si->si_addr);
2361     break;
2362   default:
2363     st->print(", si_code=%d", si->si_code);
2364     // no si_addr
2365   }
2366 
2367   if ((si->si_signo == SIGBUS || si->si_signo == SIGSEGV) &&
2368       UseSharedSpaces) {
2369     FileMapInfo* mapinfo = FileMapInfo::current_info();
2370     if (mapinfo->is_in_shared_space(si->si_addr)) {
2371       st->print("\n\nError accessing class data sharing archive."   \
2372                 " Mapped file inaccessible during execution, "      \
2373                 " possible disk/network problem.");
2374     }
2375   }
2376   st->cr();
2377 }
2378 
2379 // Moved from whole group, because we need them here for diagnostic
2380 // prints.
2381 #define OLDMAXSIGNUM 32
2382 static int Maxsignum = 0;
2383 static int *ourSigFlags = NULL;
2384 
2385 extern "C" void sigINTRHandler(int, siginfo_t*, void*);
2386 
2387 int os::Solaris::get_our_sigflags(int sig) {
2388   assert(ourSigFlags!=NULL, "signal data structure not initialized");
2389   assert(sig > 0 && sig < Maxsignum, "vm signal out of expected range");
2390   return ourSigFlags[sig];
2391 }
2392 
2393 void os::Solaris::set_our_sigflags(int sig, int flags) {
2394   assert(ourSigFlags!=NULL, "signal data structure not initialized");
2395   assert(sig > 0 && sig < Maxsignum, "vm signal out of expected range");
2396   ourSigFlags[sig] = flags;
2397 }
2398 
2399 
2400 static const char* get_signal_handler_name(address handler,
2401                                            char* buf, int buflen) {
2402   int offset;
2403   bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);
2404   if (found) {
2405     // skip directory names
2406     const char *p1, *p2;
2407     p1 = buf;
2408     size_t len = strlen(os::file_separator());
2409     while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
2410     jio_snprintf(buf, buflen, "%s+0x%x", p1, offset);
2411   } else {
2412     jio_snprintf(buf, buflen, PTR_FORMAT, handler);
2413   }
2414   return buf;
2415 }
2416 
2417 static void print_signal_handler(outputStream* st, int sig,
2418                                   char* buf, size_t buflen) {
2419   struct sigaction sa;
2420 
2421   sigaction(sig, NULL, &sa);
2422 
2423   st->print("%s: ", os::exception_name(sig, buf, buflen));
2424 
2425   address handler = (sa.sa_flags & SA_SIGINFO)
2426                   ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
2427                   : CAST_FROM_FN_PTR(address, sa.sa_handler);
2428 
2429   if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {
2430     st->print("SIG_DFL");
2431   } else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) {
2432     st->print("SIG_IGN");
2433   } else {
2434     st->print("[%s]", get_signal_handler_name(handler, buf, buflen));
2435   }
2436 
2437   st->print(", sa_mask[0]=" PTR32_FORMAT, *(uint32_t*)&sa.sa_mask);
2438 
2439   address rh = VMError::get_resetted_sighandler(sig);
2440   // May be, handler was resetted by VMError?
2441   if(rh != NULL) {
2442     handler = rh;
2443     sa.sa_flags = VMError::get_resetted_sigflags(sig);
2444   }
2445 
2446   st->print(", sa_flags="   PTR32_FORMAT, sa.sa_flags);
2447 
2448   // Check: is it our handler?
2449   if(handler == CAST_FROM_FN_PTR(address, signalHandler) ||
2450      handler == CAST_FROM_FN_PTR(address, sigINTRHandler)) {
2451     // It is our signal handler
2452     // check for flags
2453     if(sa.sa_flags != os::Solaris::get_our_sigflags(sig)) {
2454       st->print(
2455         ", flags was changed from " PTR32_FORMAT ", consider using jsig library",
2456         os::Solaris::get_our_sigflags(sig));
2457     }
2458   }
2459   st->cr();
2460 }
2461 
2462 void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
2463   st->print_cr("Signal Handlers:");
2464   print_signal_handler(st, SIGSEGV, buf, buflen);
2465   print_signal_handler(st, SIGBUS , buf, buflen);
2466   print_signal_handler(st, SIGFPE , buf, buflen);
2467   print_signal_handler(st, SIGPIPE, buf, buflen);
2468   print_signal_handler(st, SIGXFSZ, buf, buflen);
2469   print_signal_handler(st, SIGILL , buf, buflen);
2470   print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen);
2471   print_signal_handler(st, ASYNC_SIGNAL, buf, buflen);
2472   print_signal_handler(st, BREAK_SIGNAL, buf, buflen);
2473   print_signal_handler(st, SHUTDOWN1_SIGNAL , buf, buflen);
2474   print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);
2475   print_signal_handler(st, SHUTDOWN3_SIGNAL, buf, buflen);
2476   print_signal_handler(st, os::Solaris::SIGinterrupt(), buf, buflen);
2477   print_signal_handler(st, os::Solaris::SIGasync(), buf, buflen);
2478 }
2479 
2480 static char saved_jvm_path[MAXPATHLEN] = { 0 };
2481 
2482 // Find the full path to the current module, libjvm.so or libjvm_g.so
2483 void os::jvm_path(char *buf, jint buflen) {
2484   // Error checking.
2485   if (buflen < MAXPATHLEN) {
2486     assert(false, "must use a large-enough buffer");
2487     buf[0] = '\0';
2488     return;
2489   }
2490   // Lazy resolve the path to current module.
2491   if (saved_jvm_path[0] != 0) {
2492     strcpy(buf, saved_jvm_path);
2493     return;
2494   }
2495 
2496   Dl_info dlinfo;
2497   int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
2498   assert(ret != 0, "cannot locate libjvm");
2499   realpath((char *)dlinfo.dli_fname, buf);
2500 
2501   if (Arguments::created_by_gamma_launcher()) {
2502     // Support for the gamma launcher.  Typical value for buf is
2503     // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".  If "/jre/lib/" appears at
2504     // the right place in the string, then assume we are installed in a JDK and
2505     // we're done.  Otherwise, check for a JAVA_HOME environment variable and fix
2506     // up the path so it looks like libjvm.so is installed there (append a
2507     // fake suffix hotspot/libjvm.so).
2508     const char *p = buf + strlen(buf) - 1;
2509     for (int count = 0; p > buf && count < 5; ++count) {
2510       for (--p; p > buf && *p != '/'; --p)
2511         /* empty */ ;
2512     }
2513 
2514     if (strncmp(p, "/jre/lib/", 9) != 0) {
2515       // Look for JAVA_HOME in the environment.
2516       char* java_home_var = ::getenv("JAVA_HOME");
2517       if (java_home_var != NULL && java_home_var[0] != 0) {
2518         char cpu_arch[12];
2519         char* jrelib_p;
2520         int   len;
2521         sysinfo(SI_ARCHITECTURE, cpu_arch, sizeof(cpu_arch));
2522 #ifdef _LP64
2523         // If we are on sparc running a 64-bit vm, look in jre/lib/sparcv9.
2524         if (strcmp(cpu_arch, "sparc") == 0) {
2525           strcat(cpu_arch, "v9");
2526         } else if (strcmp(cpu_arch, "i386") == 0) {
2527           strcpy(cpu_arch, "amd64");
2528         }
2529 #endif
2530         // Check the current module name "libjvm.so" or "libjvm_g.so".
2531         p = strrchr(buf, '/');
2532         assert(strstr(p, "/libjvm") == p, "invalid library name");
2533         p = strstr(p, "_g") ? "_g" : "";
2534 
2535         realpath(java_home_var, buf);
2536         // determine if this is a legacy image or modules image
2537         // modules image doesn't have "jre" subdirectory
2538         len = strlen(buf);
2539         jrelib_p = buf + len;
2540         snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);
2541         if (0 != access(buf, F_OK)) {
2542           snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);
2543         }
2544 
2545         if (0 == access(buf, F_OK)) {
2546           // Use current module name "libjvm[_g].so" instead of
2547           // "libjvm"debug_only("_g")".so" since for fastdebug version
2548           // we should have "libjvm.so" but debug_only("_g") adds "_g"!
2549           len = strlen(buf);
2550           snprintf(buf + len, buflen-len, "/hotspot/libjvm%s.so", p);
2551         } else {
2552           // Go back to path of .so
2553           realpath((char *)dlinfo.dli_fname, buf);
2554         }
2555       }
2556     }
2557   }
2558 
2559   strcpy(saved_jvm_path, buf);
2560 }
2561 
2562 
2563 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2564   // no prefix required, not even "_"
2565 }
2566 
2567 
2568 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2569   // no suffix required
2570 }
2571 
2572 // This method is a copy of JDK's sysGetLastErrorString
2573 // from src/solaris/hpi/src/system_md.c
2574 
2575 size_t os::lasterror(char *buf, size_t len) {
2576 
2577   if (errno == 0)  return 0;
2578 
2579   const char *s = ::strerror(errno);
2580   size_t n = ::strlen(s);
2581   if (n >= len) {
2582     n = len - 1;
2583   }
2584   ::strncpy(buf, s, n);
2585   buf[n] = '\0';
2586   return n;
2587 }
2588 
2589 
2590 // sun.misc.Signal
2591 
2592 extern "C" {
2593   static void UserHandler(int sig, void *siginfo, void *context) {
2594     // Ctrl-C is pressed during error reporting, likely because the error
2595     // handler fails to abort. Let VM die immediately.
2596     if (sig == SIGINT && is_error_reported()) {
2597        os::die();
2598     }
2599 
2600     os::signal_notify(sig);
2601     // We do not need to reinstate the signal handler each time...
2602   }
2603 }
2604 
2605 void* os::user_handler() {
2606   return CAST_FROM_FN_PTR(void*, UserHandler);
2607 }
2608 
2609 class Semaphore : public StackObj {
2610   public:
2611     Semaphore();
2612     ~Semaphore();
2613     void signal();
2614     void wait();
2615     bool trywait();
2616     bool timedwait(unsigned int sec, int nsec);
2617   private:
2618     sema_t _semaphore;
2619 };
2620 
2621 
2622 Semaphore::Semaphore() {
2623   sema_init(&_semaphore, 0, NULL, NULL);
2624 }
2625 
2626 Semaphore::~Semaphore() {
2627   sema_destroy(&_semaphore);
2628 }
2629 
2630 void Semaphore::signal() {
2631   sema_post(&_semaphore);
2632 }
2633 
2634 void Semaphore::wait() {
2635   sema_wait(&_semaphore);
2636 }
2637 
2638 bool Semaphore::trywait() {
2639   return sema_trywait(&_semaphore) == 0;
2640 }
2641 
2642 bool Semaphore::timedwait(unsigned int sec, int nsec) {
2643   struct timespec ts;
2644   unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
2645 
2646   while (1) {
2647     int result = sema_timedwait(&_semaphore, &ts);
2648     if (result == 0) {
2649       return true;
2650     } else if (errno == EINTR) {
2651       continue;
2652     } else if (errno == ETIME) {
2653       return false;
2654     } else {
2655       return false;
2656     }
2657   }
2658 }
2659 
2660 extern "C" {
2661   typedef void (*sa_handler_t)(int);
2662   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2663 }
2664 
2665 void* os::signal(int signal_number, void* handler) {
2666   struct sigaction sigAct, oldSigAct;
2667   sigfillset(&(sigAct.sa_mask));
2668   sigAct.sa_flags = SA_RESTART & ~SA_RESETHAND;
2669   sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
2670 
2671   if (sigaction(signal_number, &sigAct, &oldSigAct))
2672     // -1 means registration failed
2673     return (void *)-1;
2674 
2675   return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
2676 }
2677 
2678 void os::signal_raise(int signal_number) {
2679   raise(signal_number);
2680 }
2681 
2682 /*
2683  * The following code is moved from os.cpp for making this
2684  * code platform specific, which it is by its very nature.
2685  */
2686 
2687 // a counter for each possible signal value
2688 static int Sigexit = 0;
2689 static int Maxlibjsigsigs;
2690 static jint *pending_signals = NULL;
2691 static int *preinstalled_sigs = NULL;
2692 static struct sigaction *chainedsigactions = NULL;
2693 static sema_t sig_sem;
2694 typedef int (*version_getting_t)();
2695 version_getting_t os::Solaris::get_libjsig_version = NULL;
2696 static int libjsigversion = NULL;
2697 
2698 int os::sigexitnum_pd() {
2699   assert(Sigexit > 0, "signal memory not yet initialized");
2700   return Sigexit;
2701 }
2702 
2703 void os::Solaris::init_signal_mem() {
2704   // Initialize signal structures
2705   Maxsignum = SIGRTMAX;
2706   Sigexit = Maxsignum+1;
2707   assert(Maxsignum >0, "Unable to obtain max signal number");
2708 
2709   Maxlibjsigsigs = Maxsignum;
2710 
2711   // pending_signals has one int per signal
2712   // The additional signal is for SIGEXIT - exit signal to signal_thread
2713   pending_signals = (jint *)os::malloc(sizeof(jint) * (Sigexit+1), mtInternal);
2714   memset(pending_signals, 0, (sizeof(jint) * (Sigexit+1)));
2715 
2716   if (UseSignalChaining) {
2717      chainedsigactions = (struct sigaction *)malloc(sizeof(struct sigaction)
2718        * (Maxsignum + 1), mtInternal);
2719      memset(chainedsigactions, 0, (sizeof(struct sigaction) * (Maxsignum + 1)));
2720      preinstalled_sigs = (int *)os::malloc(sizeof(int) * (Maxsignum + 1), mtInternal);
2721      memset(preinstalled_sigs, 0, (sizeof(int) * (Maxsignum + 1)));
2722   }
2723   ourSigFlags = (int*)malloc(sizeof(int) * (Maxsignum + 1 ), mtInternal);
2724   memset(ourSigFlags, 0, sizeof(int) * (Maxsignum + 1));
2725 }
2726 
2727 void os::signal_init_pd() {
2728   int ret;
2729 
2730   ret = ::sema_init(&sig_sem, 0, NULL, NULL);
2731   assert(ret == 0, "sema_init() failed");
2732 }
2733 
2734 void os::signal_notify(int signal_number) {
2735   int ret;
2736 
2737   Atomic::inc(&pending_signals[signal_number]);
2738   ret = ::sema_post(&sig_sem);
2739   assert(ret == 0, "sema_post() failed");
2740 }
2741 
2742 static int check_pending_signals(bool wait_for_signal) {
2743   int ret;
2744   while (true) {
2745     for (int i = 0; i < Sigexit + 1; i++) {
2746       jint n = pending_signals[i];
2747       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
2748         return i;
2749       }
2750     }
2751     if (!wait_for_signal) {
2752       return -1;
2753     }
2754     JavaThread *thread = JavaThread::current();
2755     ThreadBlockInVM tbivm(thread);
2756 
2757     bool threadIsSuspended;
2758     do {
2759       thread->set_suspend_equivalent();
2760       // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2761       while((ret = ::sema_wait(&sig_sem)) == EINTR)
2762           ;
2763       assert(ret == 0, "sema_wait() failed");
2764 
2765       // were we externally suspended while we were waiting?
2766       threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
2767       if (threadIsSuspended) {
2768         //
2769         // The semaphore has been incremented, but while we were waiting
2770         // another thread suspended us. We don't want to continue running
2771         // while suspended because that would surprise the thread that
2772         // suspended us.
2773         //
2774         ret = ::sema_post(&sig_sem);
2775         assert(ret == 0, "sema_post() failed");
2776 
2777         thread->java_suspend_self();
2778       }
2779     } while (threadIsSuspended);
2780   }
2781 }
2782 
2783 int os::signal_lookup() {
2784   return check_pending_signals(false);
2785 }
2786 
2787 int os::signal_wait() {
2788   return check_pending_signals(true);
2789 }
2790 
2791 ////////////////////////////////////////////////////////////////////////////////
2792 // Virtual Memory
2793 
2794 static int page_size = -1;
2795 
2796 // The mmap MAP_ALIGN flag is supported on Solaris 9 and later.  init_2() will
2797 // clear this var if support is not available.
2798 static bool has_map_align = true;
2799 
2800 int os::vm_page_size() {
2801   assert(page_size != -1, "must call os::init");
2802   return page_size;
2803 }
2804 
2805 // Solaris allocates memory by pages.
2806 int os::vm_allocation_granularity() {
2807   assert(page_size != -1, "must call os::init");
2808   return page_size;
2809 }
2810 
2811 static bool recoverable_mmap_error(int err) {
2812   // See if the error is one we can let the caller handle. This
2813   // list of errno values comes from the Solaris mmap(2) man page.
2814   switch (err) {
2815   case EBADF:
2816   case EINVAL:
2817   case ENOTSUP:
2818     // let the caller deal with these errors
2819     return true;
2820 
2821   default:
2822     // Any remaining errors on this OS can cause our reserved mapping
2823     // to be lost. That can cause confusion where different data
2824     // structures think they have the same memory mapped. The worst
2825     // scenario is if both the VM and a library think they have the
2826     // same memory mapped.
2827     return false;
2828   }
2829 }
2830 
2831 static void warn_fail_commit_memory(char* addr, size_t bytes, bool exec,
2832                                     int err) {
2833   warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
2834           ", %d) failed; error='%s' (errno=%d)", addr, bytes, exec,
2835           strerror(err), err);
2836 }
2837 
2838 static void warn_fail_commit_memory(char* addr, size_t bytes,
2839                                     size_t alignment_hint, bool exec,
2840                                     int err) {
2841   warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
2842           ", " SIZE_FORMAT ", %d) failed; error='%s' (errno=%d)", addr, bytes,
2843           alignment_hint, exec, strerror(err), err);
2844 }
2845 
2846 int os::Solaris::commit_memory_impl(char* addr, size_t bytes, bool exec) {
2847   int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
2848   size_t size = bytes;
2849   char *res = Solaris::mmap_chunk(addr, size, MAP_PRIVATE|MAP_FIXED, prot);
2850   if (res != NULL) {
2851     if (UseNUMAInterleaving) {
2852       numa_make_global(addr, bytes);
2853     }
2854     return 0;
2855   }
2856 
2857   int err = errno;  // save errno from mmap() call in mmap_chunk()
2858 
2859   if (!recoverable_mmap_error(err)) {
2860     warn_fail_commit_memory(addr, bytes, exec, err);
2861     vm_exit_out_of_memory(bytes, "committing reserved memory.");
2862   }
2863 
2864   return err;
2865 }
2866 
2867 bool os::pd_commit_memory(char* addr, size_t bytes, bool exec) {
2868   return Solaris::commit_memory_impl(addr, bytes, exec) == 0;
2869 }
2870 
2871 void os::pd_commit_memory_or_exit(char* addr, size_t bytes, bool exec,
2872                                   const char* mesg) {
2873   assert(mesg != NULL, "mesg must be specified");
2874   int err = os::Solaris::commit_memory_impl(addr, bytes, exec);
2875   if (err != 0) {
2876     // the caller wants all commit errors to exit with the specified mesg:
2877     warn_fail_commit_memory(addr, bytes, exec, err);
2878     vm_exit_out_of_memory(bytes, mesg);
2879   }
2880 }
2881 
2882 int os::Solaris::commit_memory_impl(char* addr, size_t bytes,
2883                                     size_t alignment_hint, bool exec) {
2884   int err = Solaris::commit_memory_impl(addr, bytes, exec);
2885   if (err == 0) {
2886     if (UseMPSS && alignment_hint > (size_t)vm_page_size()) {
2887       // If the large page size has been set and the VM
2888       // is using large pages, use the large page size
2889       // if it is smaller than the alignment hint. This is
2890       // a case where the VM wants to use a larger alignment size
2891       // for its own reasons but still want to use large pages
2892       // (which is what matters to setting the mpss range.
2893       size_t page_size = 0;
2894       if (large_page_size() < alignment_hint) {
2895         assert(UseLargePages, "Expected to be here for large page use only");
2896         page_size = large_page_size();
2897       } else {
2898         // If the alignment hint is less than the large page
2899         // size, the VM wants a particular alignment (thus the hint)
2900         // for internal reasons.  Try to set the mpss range using
2901         // the alignment_hint.
2902         page_size = alignment_hint;
2903       }
2904       // Since this is a hint, ignore any failures.
2905       (void)Solaris::set_mpss_range(addr, bytes, page_size);
2906     }
2907   }
2908   return err;
2909 }
2910 
2911 bool os::pd_commit_memory(char* addr, size_t bytes, size_t alignment_hint,
2912                           bool exec) {
2913   return Solaris::commit_memory_impl(addr, bytes, alignment_hint, exec) == 0;
2914 }
2915 
2916 void os::pd_commit_memory_or_exit(char* addr, size_t bytes,
2917                                   size_t alignment_hint, bool exec,
2918                                   const char* mesg) {
2919   assert(mesg != NULL, "mesg must be specified");
2920   int err = os::Solaris::commit_memory_impl(addr, bytes, alignment_hint, exec);
2921   if (err != 0) {
2922     // the caller wants all commit errors to exit with the specified mesg:
2923     warn_fail_commit_memory(addr, bytes, alignment_hint, exec, err);
2924     vm_exit_out_of_memory(bytes, mesg);
2925   }
2926 }
2927 
2928 // Uncommit the pages in a specified region.
2929 void os::pd_free_memory(char* addr, size_t bytes, size_t alignment_hint) {
2930   if (madvise(addr, bytes, MADV_FREE) < 0) {
2931     debug_only(warning("MADV_FREE failed."));
2932     return;
2933   }
2934 }
2935 
2936 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
2937   return os::commit_memory(addr, size, !ExecMem);
2938 }
2939 
2940 bool os::remove_stack_guard_pages(char* addr, size_t size) {
2941   return os::uncommit_memory(addr, size);
2942 }
2943 
2944 // Change the page size in a given range.
2945 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2946   assert((intptr_t)addr % alignment_hint == 0, "Address should be aligned.");
2947   assert((intptr_t)(addr + bytes) % alignment_hint == 0, "End should be aligned.");
2948   if (UseLargePages && UseMPSS) {
2949     Solaris::set_mpss_range(addr, bytes, alignment_hint);
2950   }
2951 }
2952 
2953 // Tell the OS to make the range local to the first-touching LWP
2954 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
2955   assert((intptr_t)addr % os::vm_page_size() == 0, "Address should be page-aligned.");
2956   if (madvise(addr, bytes, MADV_ACCESS_LWP) < 0) {
2957     debug_only(warning("MADV_ACCESS_LWP failed."));
2958   }
2959 }
2960 
2961 // Tell the OS that this range would be accessed from different LWPs.
2962 void os::numa_make_global(char *addr, size_t bytes) {
2963   assert((intptr_t)addr % os::vm_page_size() == 0, "Address should be page-aligned.");
2964   if (madvise(addr, bytes, MADV_ACCESS_MANY) < 0) {
2965     debug_only(warning("MADV_ACCESS_MANY failed."));
2966   }
2967 }
2968 
2969 // Get the number of the locality groups.
2970 size_t os::numa_get_groups_num() {
2971   size_t n = Solaris::lgrp_nlgrps(Solaris::lgrp_cookie());
2972   return n != -1 ? n : 1;
2973 }
2974 
2975 // Get a list of leaf locality groups. A leaf lgroup is group that
2976 // doesn't have any children. Typical leaf group is a CPU or a CPU/memory
2977 // board. An LWP is assigned to one of these groups upon creation.
2978 size_t os::numa_get_leaf_groups(int *ids, size_t size) {
2979    if ((ids[0] = Solaris::lgrp_root(Solaris::lgrp_cookie())) == -1) {
2980      ids[0] = 0;
2981      return 1;
2982    }
2983    int result_size = 0, top = 1, bottom = 0, cur = 0;
2984    for (int k = 0; k < size; k++) {
2985      int r = Solaris::lgrp_children(Solaris::lgrp_cookie(), ids[cur],
2986                                     (Solaris::lgrp_id_t*)&ids[top], size - top);
2987      if (r == -1) {
2988        ids[0] = 0;
2989        return 1;
2990      }
2991      if (!r) {
2992        // That's a leaf node.
2993        assert (bottom <= cur, "Sanity check");
2994        // Check if the node has memory
2995        if (Solaris::lgrp_resources(Solaris::lgrp_cookie(), ids[cur],
2996                                    NULL, 0, LGRP_RSRC_MEM) > 0) {
2997          ids[bottom++] = ids[cur];
2998        }
2999      }
3000      top += r;
3001      cur++;
3002    }
3003    if (bottom == 0) {
3004      // Handle a situation, when the OS reports no memory available.
3005      // Assume UMA architecture.
3006      ids[0] = 0;
3007      return 1;
3008    }
3009    return bottom;
3010 }
3011 
3012 // Detect the topology change. Typically happens during CPU plugging-unplugging.
3013 bool os::numa_topology_changed() {
3014   int is_stale = Solaris::lgrp_cookie_stale(Solaris::lgrp_cookie());
3015   if (is_stale != -1 && is_stale) {
3016     Solaris::lgrp_fini(Solaris::lgrp_cookie());
3017     Solaris::lgrp_cookie_t c = Solaris::lgrp_init(Solaris::LGRP_VIEW_CALLER);
3018     assert(c != 0, "Failure to initialize LGRP API");
3019     Solaris::set_lgrp_cookie(c);
3020     return true;
3021   }
3022   return false;
3023 }
3024 
3025 // Get the group id of the current LWP.
3026 int os::numa_get_group_id() {
3027   int lgrp_id = Solaris::lgrp_home(P_LWPID, P_MYID);
3028   if (lgrp_id == -1) {
3029     return 0;
3030   }
3031   const int size = os::numa_get_groups_num();
3032   int *ids = (int*)alloca(size * sizeof(int));
3033 
3034   // Get the ids of all lgroups with memory; r is the count.
3035   int r = Solaris::lgrp_resources(Solaris::lgrp_cookie(), lgrp_id,
3036                                   (Solaris::lgrp_id_t*)ids, size, LGRP_RSRC_MEM);
3037   if (r <= 0) {
3038     return 0;
3039   }
3040   return ids[os::random() % r];
3041 }
3042 
3043 // Request information about the page.
3044 bool os::get_page_info(char *start, page_info* info) {
3045   const uint_t info_types[] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
3046   uint64_t addr = (uintptr_t)start;
3047   uint64_t outdata[2];
3048   uint_t validity = 0;
3049 
3050   if (os::Solaris::meminfo(&addr, 1, info_types, 2, outdata, &validity) < 0) {
3051     return false;
3052   }
3053 
3054   info->size = 0;
3055   info->lgrp_id = -1;
3056 
3057   if ((validity & 1) != 0) {
3058     if ((validity & 2) != 0) {
3059       info->lgrp_id = outdata[0];
3060     }
3061     if ((validity & 4) != 0) {
3062       info->size = outdata[1];
3063     }
3064     return true;
3065   }
3066   return false;
3067 }
3068 
3069 // Scan the pages from start to end until a page different than
3070 // the one described in the info parameter is encountered.
3071 char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {
3072   const uint_t info_types[] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE };
3073   const size_t types = sizeof(info_types) / sizeof(info_types[0]);
3074   uint64_t addrs[MAX_MEMINFO_CNT], outdata[types * MAX_MEMINFO_CNT];
3075   uint_t validity[MAX_MEMINFO_CNT];
3076 
3077   size_t page_size = MAX2((size_t)os::vm_page_size(), page_expected->size);
3078   uint64_t p = (uint64_t)start;
3079   while (p < (uint64_t)end) {
3080     addrs[0] = p;
3081     size_t addrs_count = 1;
3082     while (addrs_count < MAX_MEMINFO_CNT && addrs[addrs_count - 1] < (uint64_t)end) {
3083       addrs[addrs_count] = addrs[addrs_count - 1] + page_size;
3084       addrs_count++;
3085     }
3086 
3087     if (os::Solaris::meminfo(addrs, addrs_count, info_types, types, outdata, validity) < 0) {
3088       return NULL;
3089     }
3090 
3091     size_t i = 0;
3092     for (; i < addrs_count; i++) {
3093       if ((validity[i] & 1) != 0) {
3094         if ((validity[i] & 4) != 0) {
3095           if (outdata[types * i + 1] != page_expected->size) {
3096             break;
3097           }
3098         } else
3099           if (page_expected->size != 0) {
3100             break;
3101           }
3102 
3103         if ((validity[i] & 2) != 0 && page_expected->lgrp_id > 0) {
3104           if (outdata[types * i] != page_expected->lgrp_id) {
3105             break;
3106           }
3107         }
3108       } else {
3109         return NULL;
3110       }
3111     }
3112 
3113     if (i != addrs_count) {
3114       if ((validity[i] & 2) != 0) {
3115         page_found->lgrp_id = outdata[types * i];
3116       } else {
3117         page_found->lgrp_id = -1;
3118       }
3119       if ((validity[i] & 4) != 0) {
3120         page_found->size = outdata[types * i + 1];
3121       } else {
3122         page_found->size = 0;
3123       }
3124       return (char*)addrs[i];
3125     }
3126 
3127     p = addrs[addrs_count - 1] + page_size;
3128   }
3129   return end;
3130 }
3131 
3132 bool os::pd_uncommit_memory(char* addr, size_t bytes) {
3133   size_t size = bytes;
3134   // Map uncommitted pages PROT_NONE so we fail early if we touch an
3135   // uncommitted page. Otherwise, the read/write might succeed if we
3136   // have enough swap space to back the physical page.
3137   return
3138     NULL != Solaris::mmap_chunk(addr, size,
3139                                 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE,
3140                                 PROT_NONE);
3141 }
3142 
3143 char* os::Solaris::mmap_chunk(char *addr, size_t size, int flags, int prot) {
3144   char *b = (char *)mmap(addr, size, prot, flags, os::Solaris::_dev_zero_fd, 0);
3145 
3146   if (b == MAP_FAILED) {
3147     return NULL;
3148   }
3149   return b;
3150 }
3151 
3152 char* os::Solaris::anon_mmap(char* requested_addr, size_t bytes, size_t alignment_hint, bool fixed) {
3153   char* addr = requested_addr;
3154   int flags = MAP_PRIVATE | MAP_NORESERVE;
3155 
3156   assert(!(fixed && (alignment_hint > 0)), "alignment hint meaningless with fixed mmap");
3157 
3158   if (fixed) {
3159     flags |= MAP_FIXED;
3160   } else if (has_map_align && (alignment_hint > (size_t) vm_page_size())) {
3161     flags |= MAP_ALIGN;
3162     addr = (char*) alignment_hint;
3163   }
3164 
3165   // Map uncommitted pages PROT_NONE so we fail early if we touch an
3166   // uncommitted page. Otherwise, the read/write might succeed if we
3167   // have enough swap space to back the physical page.
3168   return mmap_chunk(addr, bytes, flags, PROT_NONE);
3169 }
3170 
3171 char* os::pd_reserve_memory(size_t bytes, char* requested_addr, size_t alignment_hint) {
3172   char* addr = Solaris::anon_mmap(requested_addr, bytes, alignment_hint, (requested_addr != NULL));
3173 
3174   guarantee(requested_addr == NULL || requested_addr == addr,
3175             "OS failed to return requested mmap address.");
3176   return addr;
3177 }
3178 
3179 // Reserve memory at an arbitrary address, only if that area is
3180 // available (and not reserved for something else).
3181 
3182 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
3183   const int max_tries = 10;
3184   char* base[max_tries];
3185   size_t size[max_tries];
3186 
3187   // Solaris adds a gap between mmap'ed regions.  The size of the gap
3188   // is dependent on the requested size and the MMU.  Our initial gap
3189   // value here is just a guess and will be corrected later.
3190   bool had_top_overlap = false;
3191   bool have_adjusted_gap = false;
3192   size_t gap = 0x400000;
3193 
3194   // Assert only that the size is a multiple of the page size, since
3195   // that's all that mmap requires, and since that's all we really know
3196   // about at this low abstraction level.  If we need higher alignment,
3197   // we can either pass an alignment to this method or verify alignment
3198   // in one of the methods further up the call chain.  See bug 5044738.
3199   assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");
3200 
3201   // Since snv_84, Solaris attempts to honor the address hint - see 5003415.
3202   // Give it a try, if the kernel honors the hint we can return immediately.
3203   char* addr = Solaris::anon_mmap(requested_addr, bytes, 0, false);
3204 
3205   volatile int err = errno;
3206   if (addr == requested_addr) {
3207     return addr;
3208   } else if (addr != NULL) {
3209     pd_unmap_memory(addr, bytes);
3210   }
3211 
3212   if (PrintMiscellaneous && Verbose) {
3213     char buf[256];
3214     buf[0] = '\0';
3215     if (addr == NULL) {
3216       jio_snprintf(buf, sizeof(buf), ": %s", strerror(err));
3217     }
3218     warning("attempt_reserve_memory_at: couldn't reserve " SIZE_FORMAT " bytes at "
3219             PTR_FORMAT ": reserve_memory_helper returned " PTR_FORMAT
3220             "%s", bytes, requested_addr, addr, buf);
3221   }
3222 
3223   // Address hint method didn't work.  Fall back to the old method.
3224   // In theory, once SNV becomes our oldest supported platform, this
3225   // code will no longer be needed.
3226   //
3227   // Repeatedly allocate blocks until the block is allocated at the
3228   // right spot. Give up after max_tries.
3229   int i;
3230   for (i = 0; i < max_tries; ++i) {
3231     base[i] = reserve_memory(bytes);
3232 
3233     if (base[i] != NULL) {
3234       // Is this the block we wanted?
3235       if (base[i] == requested_addr) {
3236         size[i] = bytes;
3237         break;
3238       }
3239 
3240       // check that the gap value is right
3241       if (had_top_overlap && !have_adjusted_gap) {
3242         size_t actual_gap = base[i-1] - base[i] - bytes;
3243         if (gap != actual_gap) {
3244           // adjust the gap value and retry the last 2 allocations
3245           assert(i > 0, "gap adjustment code problem");
3246           have_adjusted_gap = true;  // adjust the gap only once, just in case
3247           gap = actual_gap;
3248           if (PrintMiscellaneous && Verbose) {
3249             warning("attempt_reserve_memory_at: adjusted gap to 0x%lx", gap);
3250           }
3251           unmap_memory(base[i], bytes);
3252           unmap_memory(base[i-1], size[i-1]);
3253           i-=2;
3254           continue;
3255         }
3256       }
3257 
3258       // Does this overlap the block we wanted? Give back the overlapped
3259       // parts and try again.
3260       //
3261       // There is still a bug in this code: if top_overlap == bytes,
3262       // the overlap is offset from requested region by the value of gap.
3263       // In this case giving back the overlapped part will not work,
3264       // because we'll give back the entire block at base[i] and
3265       // therefore the subsequent allocation will not generate a new gap.
3266       // This could be fixed with a new algorithm that used larger
3267       // or variable size chunks to find the requested region -
3268       // but such a change would introduce additional complications.
3269       // It's rare enough that the planets align for this bug,
3270       // so we'll just wait for a fix for 6204603/5003415 which
3271       // will provide a mmap flag to allow us to avoid this business.
3272 
3273       size_t top_overlap = requested_addr + (bytes + gap) - base[i];
3274       if (top_overlap >= 0 && top_overlap < bytes) {
3275         had_top_overlap = true;
3276         unmap_memory(base[i], top_overlap);
3277         base[i] += top_overlap;
3278         size[i] = bytes - top_overlap;
3279       } else {
3280         size_t bottom_overlap = base[i] + bytes - requested_addr;
3281         if (bottom_overlap >= 0 && bottom_overlap < bytes) {
3282           if (PrintMiscellaneous && Verbose && bottom_overlap == 0) {
3283             warning("attempt_reserve_memory_at: possible alignment bug");
3284           }
3285           unmap_memory(requested_addr, bottom_overlap);
3286           size[i] = bytes - bottom_overlap;
3287         } else {
3288           size[i] = bytes;
3289         }
3290       }
3291     }
3292   }
3293 
3294   // Give back the unused reserved pieces.
3295 
3296   for (int j = 0; j < i; ++j) {
3297     if (base[j] != NULL) {
3298       unmap_memory(base[j], size[j]);
3299     }
3300   }
3301 
3302   return (i < max_tries) ? requested_addr : NULL;
3303 }
3304 
3305 bool os::pd_release_memory(char* addr, size_t bytes) {
3306   size_t size = bytes;
3307   return munmap(addr, size) == 0;
3308 }
3309 
3310 static bool solaris_mprotect(char* addr, size_t bytes, int prot) {
3311   assert(addr == (char*)align_size_down((uintptr_t)addr, os::vm_page_size()),
3312          "addr must be page aligned");
3313   int retVal = mprotect(addr, bytes, prot);
3314   return retVal == 0;
3315 }
3316 
3317 // Protect memory (Used to pass readonly pages through
3318 // JNI GetArray<type>Elements with empty arrays.)
3319 // Also, used for serialization page and for compressed oops null pointer
3320 // checking.
3321 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
3322                         bool is_committed) {
3323   unsigned int p = 0;
3324   switch (prot) {
3325   case MEM_PROT_NONE: p = PROT_NONE; break;
3326   case MEM_PROT_READ: p = PROT_READ; break;
3327   case MEM_PROT_RW:   p = PROT_READ|PROT_WRITE; break;
3328   case MEM_PROT_RWX:  p = PROT_READ|PROT_WRITE|PROT_EXEC; break;
3329   default:
3330     ShouldNotReachHere();
3331   }
3332   // is_committed is unused.
3333   return solaris_mprotect(addr, bytes, p);
3334 }
3335 
3336 // guard_memory and unguard_memory only happens within stack guard pages.
3337 // Since ISM pertains only to the heap, guard and unguard memory should not
3338 /// happen with an ISM region.
3339 bool os::guard_memory(char* addr, size_t bytes) {
3340   return solaris_mprotect(addr, bytes, PROT_NONE);
3341 }
3342 
3343 bool os::unguard_memory(char* addr, size_t bytes) {
3344   return solaris_mprotect(addr, bytes, PROT_READ|PROT_WRITE);
3345 }
3346 
3347 // Large page support
3348 
3349 // UseLargePages is the master flag to enable/disable large page memory.
3350 // UseMPSS and UseISM are supported for compatibility reasons. Their combined
3351 // effects can be described in the following table:
3352 //
3353 // UseLargePages UseMPSS UseISM
3354 //    false         *       *   => UseLargePages is the master switch, turning
3355 //                                 it off will turn off both UseMPSS and
3356 //                                 UseISM. VM will not use large page memory
3357 //                                 regardless the settings of UseMPSS/UseISM.
3358 //     true      false    false => Unless future Solaris provides other
3359 //                                 mechanism to use large page memory, this
3360 //                                 combination is equivalent to -UseLargePages,
3361 //                                 VM will not use large page memory
3362 //     true      true     false => JVM will use MPSS for large page memory.
3363 //                                 This is the default behavior.
3364 //     true      false    true  => JVM will use ISM for large page memory.
3365 //     true      true     true  => JVM will use ISM if it is available.
3366 //                                 Otherwise, JVM will fall back to MPSS.
3367 //                                 Becaues ISM is now available on all
3368 //                                 supported Solaris versions, this combination
3369 //                                 is equivalent to +UseISM -UseMPSS.
3370 
3371 static size_t _large_page_size = 0;
3372 
3373 bool os::Solaris::ism_sanity_check(bool warn, size_t * page_size) {
3374   // x86 uses either 2M or 4M page, depending on whether PAE (Physical Address
3375   // Extensions) mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. Sparc
3376   // can support multiple page sizes.
3377 
3378   // Don't bother to probe page size because getpagesizes() comes with MPSS.
3379   // ISM is only recommended on old Solaris where there is no MPSS support.
3380   // Simply choose a conservative value as default.
3381   *page_size = LargePageSizeInBytes ? LargePageSizeInBytes :
3382                SPARC_ONLY(4 * M) IA32_ONLY(4 * M) AMD64_ONLY(2 * M)
3383                ARM_ONLY(2 * M);
3384 
3385   // ISM is available on all supported Solaris versions
3386   return true;
3387 }
3388 
3389 // Insertion sort for small arrays (descending order).
3390 static void insertion_sort_descending(size_t* array, int len) {
3391   for (int i = 0; i < len; i++) {
3392     size_t val = array[i];
3393     for (size_t key = i; key > 0 && array[key - 1] < val; --key) {
3394       size_t tmp = array[key];
3395       array[key] = array[key - 1];
3396       array[key - 1] = tmp;
3397     }
3398   }
3399 }
3400 
3401 bool os::Solaris::mpss_sanity_check(bool warn, size_t * page_size) {
3402   const unsigned int usable_count = VM_Version::page_size_count();
3403   if (usable_count == 1) {
3404     return false;
3405   }
3406 
3407   // Find the right getpagesizes interface.  When solaris 11 is the minimum
3408   // build platform, getpagesizes() (without the '2') can be called directly.
3409   typedef int (*gps_t)(size_t[], int);
3410   gps_t gps_func = CAST_TO_FN_PTR(gps_t, dlsym(RTLD_DEFAULT, "getpagesizes2"));
3411   if (gps_func == NULL) {
3412     gps_func = CAST_TO_FN_PTR(gps_t, dlsym(RTLD_DEFAULT, "getpagesizes"));
3413     if (gps_func == NULL) {
3414       if (warn) {
3415         warning("MPSS is not supported by the operating system.");
3416       }
3417       return false;
3418     }
3419   }
3420 
3421   // Fill the array of page sizes.
3422   int n = (*gps_func)(_page_sizes, page_sizes_max);
3423   assert(n > 0, "Solaris bug?");
3424 
3425   if (n == page_sizes_max) {
3426     // Add a sentinel value (necessary only if the array was completely filled
3427     // since it is static (zeroed at initialization)).
3428     _page_sizes[--n] = 0;
3429     DEBUG_ONLY(warning("increase the size of the os::_page_sizes array.");)
3430   }
3431   assert(_page_sizes[n] == 0, "missing sentinel");
3432   trace_page_sizes("available page sizes", _page_sizes, n);
3433 
3434   if (n == 1) return false;     // Only one page size available.
3435 
3436   // Skip sizes larger than 4M (or LargePageSizeInBytes if it was set) and
3437   // select up to usable_count elements.  First sort the array, find the first
3438   // acceptable value, then copy the usable sizes to the top of the array and
3439   // trim the rest.  Make sure to include the default page size :-).
3440   //
3441   // A better policy could get rid of the 4M limit by taking the sizes of the
3442   // important VM memory regions (java heap and possibly the code cache) into
3443   // account.
3444   insertion_sort_descending(_page_sizes, n);
3445   const size_t size_limit =
3446     FLAG_IS_DEFAULT(LargePageSizeInBytes) ? 4 * M : LargePageSizeInBytes;
3447   int beg;
3448   for (beg = 0; beg < n && _page_sizes[beg] > size_limit; ++beg) /* empty */ ;
3449   const int end = MIN2((int)usable_count, n) - 1;
3450   for (int cur = 0; cur < end; ++cur, ++beg) {
3451     _page_sizes[cur] = _page_sizes[beg];
3452   }
3453   _page_sizes[end] = vm_page_size();
3454   _page_sizes[end + 1] = 0;
3455 
3456   if (_page_sizes[end] > _page_sizes[end - 1]) {
3457     // Default page size is not the smallest; sort again.
3458     insertion_sort_descending(_page_sizes, end + 1);
3459   }
3460   *page_size = _page_sizes[0];
3461 
3462   trace_page_sizes("usable page sizes", _page_sizes, end + 1);
3463   return true;
3464 }
3465 
3466 void os::large_page_init() {
3467   if (!UseLargePages) {
3468     UseISM = false;
3469     UseMPSS = false;
3470     return;
3471   }
3472 
3473   // print a warning if any large page related flag is specified on command line
3474   bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages)        ||
3475                          !FLAG_IS_DEFAULT(UseISM)               ||
3476                          !FLAG_IS_DEFAULT(UseMPSS)              ||
3477                          !FLAG_IS_DEFAULT(LargePageSizeInBytes);
3478   UseISM = UseISM &&
3479            Solaris::ism_sanity_check(warn_on_failure, &_large_page_size);
3480   if (UseISM) {
3481     // ISM disables MPSS to be compatible with old JDK behavior
3482     UseMPSS = false;
3483     _page_sizes[0] = _large_page_size;
3484     _page_sizes[1] = vm_page_size();
3485   }
3486 
3487   UseMPSS = UseMPSS &&
3488             Solaris::mpss_sanity_check(warn_on_failure, &_large_page_size);
3489 
3490   UseLargePages = UseISM || UseMPSS;
3491 }
3492 
3493 bool os::Solaris::set_mpss_range(caddr_t start, size_t bytes, size_t align) {
3494   // Signal to OS that we want large pages for addresses
3495   // from addr, addr + bytes
3496   struct memcntl_mha mpss_struct;
3497   mpss_struct.mha_cmd = MHA_MAPSIZE_VA;
3498   mpss_struct.mha_pagesize = align;
3499   mpss_struct.mha_flags = 0;
3500   if (memcntl(start, bytes, MC_HAT_ADVISE,
3501               (caddr_t) &mpss_struct, 0, 0) < 0) {
3502     debug_only(warning("Attempt to use MPSS failed."));
3503     return false;
3504   }
3505   return true;
3506 }
3507 
3508 char* os::reserve_memory_special(size_t size, size_t alignment, char* addr, bool exec) {
3509   // "exec" is passed in but not used.  Creating the shared image for
3510   // the code cache doesn't have an SHM_X executable permission to check.
3511   assert(UseLargePages && UseISM, "only for ISM large pages");
3512 
3513   if (!is_size_aligned(size, os::large_page_size()) || alignment > os::large_page_size()) {
3514     return NULL; // Fallback to small pages.
3515   }
3516 
3517   char* retAddr = NULL;
3518   int shmid;
3519   key_t ismKey;
3520 
3521   bool warn_on_failure = UseISM &&
3522                         (!FLAG_IS_DEFAULT(UseLargePages)         ||
3523                          !FLAG_IS_DEFAULT(UseISM)                ||
3524                          !FLAG_IS_DEFAULT(LargePageSizeInBytes)
3525                         );
3526   char msg[128];
3527 
3528   ismKey = IPC_PRIVATE;
3529 
3530   // Create a large shared memory region to attach to based on size.
3531   // Currently, size is the total size of the heap
3532   shmid = shmget(ismKey, size, SHM_R | SHM_W | IPC_CREAT);
3533   if (shmid == -1){
3534      if (warn_on_failure) {
3535        jio_snprintf(msg, sizeof(msg), "Failed to reserve shared memory (errno = %d).", errno);
3536        warning(msg);
3537      }
3538      return NULL;
3539   }
3540 
3541   // Attach to the region
3542   retAddr = (char *) shmat(shmid, 0, SHM_SHARE_MMU | SHM_R | SHM_W);
3543   int err = errno;
3544 
3545   // Remove shmid. If shmat() is successful, the actual shared memory segment
3546   // will be deleted when it's detached by shmdt() or when the process
3547   // terminates. If shmat() is not successful this will remove the shared
3548   // segment immediately.
3549   shmctl(shmid, IPC_RMID, NULL);
3550 
3551   if (retAddr == (char *) -1) {
3552     if (warn_on_failure) {
3553       jio_snprintf(msg, sizeof(msg), "Failed to attach shared memory (errno = %d).", err);
3554       warning(msg);
3555     }
3556     return NULL;
3557   }
3558   if ((retAddr != NULL) && UseNUMAInterleaving) {
3559     numa_make_global(retAddr, size);
3560   }
3561 
3562   // The memory is committed
3563   MemTracker::record_virtual_memory_reserve_and_commit((address)retAddr, size, mtNone, CURRENT_PC);
3564 
3565   return retAddr;
3566 }
3567 
3568 bool os::release_memory_special(char* base, size_t bytes) {
3569   MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
3570   // detaching the SHM segment will also delete it, see reserve_memory_special()
3571   int rslt = shmdt(base);
3572   if (rslt == 0) {
3573     tkr.record((address)base, bytes);
3574     return true;
3575   } else {
3576     tkr.discard();
3577    return false;
3578   }
3579 }
3580 
3581 size_t os::large_page_size() {
3582   return _large_page_size;
3583 }
3584 
3585 // MPSS allows application to commit large page memory on demand; with ISM
3586 // the entire memory region must be allocated as shared memory.
3587 bool os::can_commit_large_page_memory() {
3588   return UseISM ? false : true;
3589 }
3590 
3591 bool os::can_execute_large_page_memory() {
3592   return UseISM ? false : true;
3593 }
3594 
3595 static int os_sleep(jlong millis, bool interruptible) {
3596   const jlong limit = INT_MAX;
3597   jlong prevtime;
3598   int res;
3599 
3600   while (millis > limit) {
3601     if ((res = os_sleep(limit, interruptible)) != OS_OK)
3602       return res;
3603     millis -= limit;
3604   }
3605 
3606   // Restart interrupted polls with new parameters until the proper delay
3607   // has been completed.
3608 
3609   prevtime = getTimeMillis();
3610 
3611   while (millis > 0) {
3612     jlong newtime;
3613 
3614     if (!interruptible) {
3615       // Following assert fails for os::yield_all:
3616       // assert(!thread->is_Java_thread(), "must not be java thread");
3617       res = poll(NULL, 0, millis);
3618     } else {
3619       JavaThread *jt = JavaThread::current();
3620 
3621       INTERRUPTIBLE_NORESTART_VM_ALWAYS(poll(NULL, 0, millis), res, jt,
3622         os::Solaris::clear_interrupted);
3623     }
3624 
3625     // INTERRUPTIBLE_NORESTART_VM_ALWAYS returns res == OS_INTRPT for
3626     // thread.Interrupt.
3627 
3628     // See c/r 6751923. Poll can return 0 before time
3629     // has elapsed if time is set via clock_settime (as NTP does).
3630     // res == 0 if poll timed out (see man poll RETURN VALUES)
3631     // using the logic below checks that we really did
3632     // sleep at least "millis" if not we'll sleep again.
3633     if( ( res == 0 ) || ((res == OS_ERR) && (errno == EINTR))) {
3634       newtime = getTimeMillis();
3635       assert(newtime >= prevtime, "time moving backwards");
3636     /* Doing prevtime and newtime in microseconds doesn't help precision,
3637        and trying to round up to avoid lost milliseconds can result in a
3638        too-short delay. */
3639       millis -= newtime - prevtime;
3640       if(millis <= 0)
3641         return OS_OK;
3642       prevtime = newtime;
3643     } else
3644       return res;
3645   }
3646 
3647   return OS_OK;
3648 }
3649 
3650 // Read calls from inside the vm need to perform state transitions
3651 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3652   INTERRUPTIBLE_RETURN_INT_VM(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
3653 }
3654 
3655 size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
3656   INTERRUPTIBLE_RETURN_INT(::read(fd, buf, nBytes), os::Solaris::clear_interrupted);
3657 }
3658 
3659 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
3660   assert(thread == Thread::current(),  "thread consistency check");
3661 
3662   // TODO-FIXME: this should be removed.
3663   // On Solaris machines (especially 2.5.1) we found that sometimes the VM gets into a live lock
3664   // situation with a JavaThread being starved out of a lwp. The kernel doesn't seem to generate
3665   // a SIGWAITING signal which would enable the threads library to create a new lwp for the starving
3666   // thread. We suspect that because the Watcher thread keeps waking up at periodic intervals the kernel
3667   // is fooled into believing that the system is making progress. In the code below we block the
3668   // the watcher thread while safepoint is in progress so that it would not appear as though the
3669   // system is making progress.
3670   if (!Solaris::T2_libthread() &&
3671       thread->is_Watcher_thread() && SafepointSynchronize::is_synchronizing() && !Arguments::has_profile()) {
3672     // We now try to acquire the threads lock. Since this lock is held by the VM thread during
3673     // the entire safepoint, the watcher thread will  line up here during the safepoint.
3674     Threads_lock->lock_without_safepoint_check();
3675     Threads_lock->unlock();
3676   }
3677 
3678   if (thread->is_Java_thread()) {
3679     // This is a JavaThread so we honor the _thread_blocked protocol
3680     // even for sleeps of 0 milliseconds. This was originally done
3681     // as a workaround for bug 4338139. However, now we also do it
3682     // to honor the suspend-equivalent protocol.
3683 
3684     JavaThread *jt = (JavaThread *) thread;
3685     ThreadBlockInVM tbivm(jt);
3686 
3687     jt->set_suspend_equivalent();
3688     // cleared by handle_special_suspend_equivalent_condition() or
3689     // java_suspend_self() via check_and_wait_while_suspended()
3690 
3691     int ret_code;
3692     if (millis <= 0) {
3693       thr_yield();
3694       ret_code = 0;
3695     } else {
3696       // The original sleep() implementation did not create an
3697       // OSThreadWaitState helper for sleeps of 0 milliseconds.
3698       // I'm preserving that decision for now.
3699       OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
3700 
3701       ret_code = os_sleep(millis, interruptible);
3702     }
3703 
3704     // were we externally suspended while we were waiting?
3705     jt->check_and_wait_while_suspended();
3706 
3707     return ret_code;
3708   }
3709 
3710   // non-JavaThread from this point on:
3711 
3712   if (millis <= 0) {
3713     thr_yield();
3714     return 0;
3715   }
3716 
3717   OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
3718 
3719   return os_sleep(millis, interruptible);
3720 }
3721 
3722 int os::naked_sleep() {
3723   // %% make the sleep time an integer flag. for now use 1 millisec.
3724   return os_sleep(1, false);
3725 }
3726 
3727 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3728 void os::infinite_sleep() {
3729   while (true) {    // sleep forever ...
3730     ::sleep(100);   // ... 100 seconds at a time
3731   }
3732 }
3733 
3734 // Used to convert frequent JVM_Yield() to nops
3735 bool os::dont_yield() {
3736   if (DontYieldALot) {
3737     static hrtime_t last_time = 0;
3738     hrtime_t diff = getTimeNanos() - last_time;
3739 
3740     if (diff < DontYieldALotInterval * 1000000)
3741       return true;
3742 
3743     last_time += diff;
3744 
3745     return false;
3746   }
3747   else {
3748     return false;
3749   }
3750 }
3751 
3752 // Caveat: Solaris os::yield() causes a thread-state transition whereas
3753 // the linux and win32 implementations do not.  This should be checked.
3754 
3755 void os::yield() {
3756   // Yields to all threads with same or greater priority
3757   os::sleep(Thread::current(), 0, false);
3758 }
3759 
3760 // Note that yield semantics are defined by the scheduling class to which
3761 // the thread currently belongs.  Typically, yield will _not yield to
3762 // other equal or higher priority threads that reside on the dispatch queues
3763 // of other CPUs.
3764 
3765 os::YieldResult os::NakedYield() { thr_yield(); return os::YIELD_UNKNOWN; }
3766 
3767 
3768 // On Solaris we found that yield_all doesn't always yield to all other threads.
3769 // There have been cases where there is a thread ready to execute but it doesn't
3770 // get an lwp as the VM thread continues to spin with sleeps of 1 millisecond.
3771 // The 1 millisecond wait doesn't seem long enough for the kernel to issue a
3772 // SIGWAITING signal which will cause a new lwp to be created. So we count the
3773 // number of times yield_all is called in the one loop and increase the sleep
3774 // time after 8 attempts. If this fails too we increase the concurrency level
3775 // so that the starving thread would get an lwp
3776 
3777 void os::yield_all(int attempts) {
3778   // Yields to all threads, including threads with lower priorities
3779   if (attempts == 0) {
3780     os::sleep(Thread::current(), 1, false);
3781   } else {
3782     int iterations = attempts % 30;
3783     if (iterations == 0 && !os::Solaris::T2_libthread()) {
3784       // thr_setconcurrency and _getconcurrency make sense only under T1.
3785       int noofLWPS = thr_getconcurrency();
3786       if (noofLWPS < (Threads::number_of_threads() + 2)) {
3787         thr_setconcurrency(thr_getconcurrency() + 1);
3788       }
3789     } else if (iterations < 25) {
3790       os::sleep(Thread::current(), 1, false);
3791     } else {
3792       os::sleep(Thread::current(), 10, false);
3793     }
3794   }
3795 }
3796 
3797 // Called from the tight loops to possibly influence time-sharing heuristics
3798 void os::loop_breaker(int attempts) {
3799   os::yield_all(attempts);
3800 }
3801 
3802 
3803 // Interface for setting lwp priorities.  If we are using T2 libthread,
3804 // which forces the use of BoundThreads or we manually set UseBoundThreads,
3805 // all of our threads will be assigned to real lwp's.  Using the thr_setprio
3806 // function is meaningless in this mode so we must adjust the real lwp's priority
3807 // The routines below implement the getting and setting of lwp priorities.
3808 //
3809 // Note: There are three priority scales used on Solaris.  Java priotities
3810 //       which range from 1 to 10, libthread "thr_setprio" scale which range
3811 //       from 0 to 127, and the current scheduling class of the process we
3812 //       are running in.  This is typically from -60 to +60.
3813 //       The setting of the lwp priorities in done after a call to thr_setprio
3814 //       so Java priorities are mapped to libthread priorities and we map from
3815 //       the latter to lwp priorities.  We don't keep priorities stored in
3816 //       Java priorities since some of our worker threads want to set priorities
3817 //       higher than all Java threads.
3818 //
3819 // For related information:
3820 // (1)  man -s 2 priocntl
3821 // (2)  man -s 4 priocntl
3822 // (3)  man dispadmin
3823 // =    librt.so
3824 // =    libthread/common/rtsched.c - thrp_setlwpprio().
3825 // =    ps -cL <pid> ... to validate priority.
3826 // =    sched_get_priority_min and _max
3827 //              pthread_create
3828 //              sched_setparam
3829 //              pthread_setschedparam
3830 //
3831 // Assumptions:
3832 // +    We assume that all threads in the process belong to the same
3833 //              scheduling class.   IE. an homogenous process.
3834 // +    Must be root or in IA group to change change "interactive" attribute.
3835 //              Priocntl() will fail silently.  The only indication of failure is when
3836 //              we read-back the value and notice that it hasn't changed.
3837 // +    Interactive threads enter the runq at the head, non-interactive at the tail.
3838 // +    For RT, change timeslice as well.  Invariant:
3839 //              constant "priority integral"
3840 //              Konst == TimeSlice * (60-Priority)
3841 //              Given a priority, compute appropriate timeslice.
3842 // +    Higher numerical values have higher priority.
3843 
3844 // sched class attributes
3845 typedef struct {
3846         int   schedPolicy;              // classID
3847         int   maxPrio;
3848         int   minPrio;
3849 } SchedInfo;
3850 
3851 
3852 static SchedInfo tsLimits, iaLimits, rtLimits, fxLimits;
3853 
3854 #ifdef ASSERT
3855 static int  ReadBackValidate = 1;
3856 #endif
3857 static int  myClass     = 0;
3858 static int  myMin       = 0;
3859 static int  myMax       = 0;
3860 static int  myCur       = 0;
3861 static bool priocntl_enable = false;
3862 
3863 static const int criticalPrio = 60; // FX/60 is critical thread class/priority on T4
3864 static int java_MaxPriority_to_os_priority = 0; // Saved mapping
3865 
3866 // Call the version of priocntl suitable for all supported versions
3867 // of Solaris. We need to call through this wrapper so that we can
3868 // build on Solaris 9 and run on Solaris 8, 9 and 10.
3869 //
3870 // This code should be removed if we ever stop supporting Solaris 8
3871 // and earlier releases.
3872 
3873 static long priocntl_stub(int pcver, idtype_t idtype, id_t id, int cmd, caddr_t arg);
3874 typedef long (*priocntl_type)(int pcver, idtype_t idtype, id_t id, int cmd, caddr_t arg);
3875 static priocntl_type priocntl_ptr = priocntl_stub;
3876 
3877 // Stub to set the value of the real pointer, and then call the real
3878 // function.
3879 
3880 static long priocntl_stub(int pcver, idtype_t idtype, id_t id, int cmd, caddr_t arg) {
3881   // Try Solaris 8- name only.
3882   priocntl_type tmp = (priocntl_type)dlsym(RTLD_DEFAULT, "__priocntl");
3883   guarantee(tmp != NULL, "priocntl function not found.");
3884   priocntl_ptr = tmp;
3885   return (*priocntl_ptr)(PC_VERSION, idtype, id, cmd, arg);
3886 }
3887 
3888 
3889 // lwp_priocntl_init
3890 //
3891 // Try to determine the priority scale for our process.
3892 //
3893 // Return errno or 0 if OK.
3894 //
3895 static
3896 int     lwp_priocntl_init ()
3897 {
3898   int rslt;
3899   pcinfo_t ClassInfo;
3900   pcparms_t ParmInfo;
3901   int i;
3902 
3903   if (!UseThreadPriorities) return 0;
3904 
3905   // We are using Bound threads, we need to determine our priority ranges
3906   if (os::Solaris::T2_libthread() || UseBoundThreads) {
3907     // If ThreadPriorityPolicy is 1, switch tables
3908     if (ThreadPriorityPolicy == 1) {
3909       for (i = 0 ; i < CriticalPriority+1; i++)
3910         os::java_to_os_priority[i] = prio_policy1[i];
3911     }
3912     if (UseCriticalJavaThreadPriority) {
3913       // MaxPriority always maps to the FX scheduling class and criticalPrio.
3914       // See set_native_priority() and set_lwp_class_and_priority().
3915       // Save original MaxPriority mapping in case attempt to
3916       // use critical priority fails.
3917       java_MaxPriority_to_os_priority = os::java_to_os_priority[MaxPriority];
3918       // Set negative to distinguish from other priorities
3919       os::java_to_os_priority[MaxPriority] = -criticalPrio;
3920     }
3921   }
3922   // Not using Bound Threads, set to ThreadPolicy 1
3923   else {
3924     for ( i = 0 ; i < CriticalPriority+1; i++ ) {
3925       os::java_to_os_priority[i] = prio_policy1[i];
3926     }
3927     return 0;
3928   }
3929 
3930   // Get IDs for a set of well-known scheduling classes.
3931   // TODO-FIXME: GETCLINFO returns the current # of classes in the
3932   // the system.  We should have a loop that iterates over the
3933   // classID values, which are known to be "small" integers.
3934 
3935   strcpy(ClassInfo.pc_clname, "TS");
3936   ClassInfo.pc_cid = -1;
3937   rslt = (*priocntl_ptr)(PC_VERSION, P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3938   if (rslt < 0) return errno;
3939   assert(ClassInfo.pc_cid != -1, "cid for TS class is -1");
3940   tsLimits.schedPolicy = ClassInfo.pc_cid;
3941   tsLimits.maxPrio = ((tsinfo_t*)ClassInfo.pc_clinfo)->ts_maxupri;
3942   tsLimits.minPrio = -tsLimits.maxPrio;
3943 
3944   strcpy(ClassInfo.pc_clname, "IA");
3945   ClassInfo.pc_cid = -1;
3946   rslt = (*priocntl_ptr)(PC_VERSION, P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3947   if (rslt < 0) return errno;
3948   assert(ClassInfo.pc_cid != -1, "cid for IA class is -1");
3949   iaLimits.schedPolicy = ClassInfo.pc_cid;
3950   iaLimits.maxPrio = ((iainfo_t*)ClassInfo.pc_clinfo)->ia_maxupri;
3951   iaLimits.minPrio = -iaLimits.maxPrio;
3952 
3953   strcpy(ClassInfo.pc_clname, "RT");
3954   ClassInfo.pc_cid = -1;
3955   rslt = (*priocntl_ptr)(PC_VERSION, P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3956   if (rslt < 0) return errno;
3957   assert(ClassInfo.pc_cid != -1, "cid for RT class is -1");
3958   rtLimits.schedPolicy = ClassInfo.pc_cid;
3959   rtLimits.maxPrio = ((rtinfo_t*)ClassInfo.pc_clinfo)->rt_maxpri;
3960   rtLimits.minPrio = 0;
3961 
3962   strcpy(ClassInfo.pc_clname, "FX");
3963   ClassInfo.pc_cid = -1;
3964   rslt = (*priocntl_ptr)(PC_VERSION, P_ALL, 0, PC_GETCID, (caddr_t)&ClassInfo);
3965   if (rslt < 0) return errno;
3966   assert(ClassInfo.pc_cid != -1, "cid for FX class is -1");
3967   fxLimits.schedPolicy = ClassInfo.pc_cid;
3968   fxLimits.maxPrio = ((fxinfo_t*)ClassInfo.pc_clinfo)->fx_maxupri;
3969   fxLimits.minPrio = 0;
3970 
3971   // Query our "current" scheduling class.
3972   // This will normally be IA, TS or, rarely, FX or RT.
3973   memset(&ParmInfo, 0, sizeof(ParmInfo));
3974   ParmInfo.pc_cid = PC_CLNULL;
3975   rslt = (*priocntl_ptr) (PC_VERSION, P_PID, P_MYID, PC_GETPARMS, (caddr_t)&ParmInfo);
3976   if (rslt < 0) return errno;
3977   myClass = ParmInfo.pc_cid;
3978 
3979   // We now know our scheduling classId, get specific information
3980   // about the class.
3981   ClassInfo.pc_cid = myClass;
3982   ClassInfo.pc_clname[0] = 0;
3983   rslt = (*priocntl_ptr) (PC_VERSION, (idtype)0, 0, PC_GETCLINFO, (caddr_t)&ClassInfo);
3984   if (rslt < 0) return errno;
3985 
3986   if (ThreadPriorityVerbose) {
3987     tty->print_cr("lwp_priocntl_init: Class=%d(%s)...", myClass, ClassInfo.pc_clname);
3988   }
3989 
3990   memset(&ParmInfo, 0, sizeof(pcparms_t));
3991   ParmInfo.pc_cid = PC_CLNULL;
3992   rslt = (*priocntl_ptr)(PC_VERSION, P_PID, P_MYID, PC_GETPARMS, (caddr_t)&ParmInfo);
3993   if (rslt < 0) return errno;
3994 
3995   if (ParmInfo.pc_cid == rtLimits.schedPolicy) {
3996     myMin = rtLimits.minPrio;
3997     myMax = rtLimits.maxPrio;
3998   } else if (ParmInfo.pc_cid == iaLimits.schedPolicy) {
3999     iaparms_t *iaInfo  = (iaparms_t*)ParmInfo.pc_clparms;
4000     myMin = iaLimits.minPrio;
4001     myMax = iaLimits.maxPrio;
4002     myMax = MIN2(myMax, (int)iaInfo->ia_uprilim);       // clamp - restrict
4003   } else if (ParmInfo.pc_cid == tsLimits.schedPolicy) {
4004     tsparms_t *tsInfo  = (tsparms_t*)ParmInfo.pc_clparms;
4005     myMin = tsLimits.minPrio;
4006     myMax = tsLimits.maxPrio;
4007     myMax = MIN2(myMax, (int)tsInfo->ts_uprilim);       // clamp - restrict
4008   } else if (ParmInfo.pc_cid == fxLimits.schedPolicy) {
4009     fxparms_t *fxInfo = (fxparms_t*)ParmInfo.pc_clparms;
4010     myMin = fxLimits.minPrio;
4011     myMax = fxLimits.maxPrio;
4012     myMax = MIN2(myMax, (int)fxInfo->fx_uprilim);       // clamp - restrict
4013   } else {
4014     // No clue - punt
4015     if (ThreadPriorityVerbose)
4016       tty->print_cr ("Unknown scheduling class: %s ... \n", ClassInfo.pc_clname);
4017     return EINVAL;      // no clue, punt
4018   }
4019 
4020   if (ThreadPriorityVerbose) {
4021     tty->print_cr ("Thread priority Range: [%d..%d]\n", myMin, myMax);
4022   }
4023 
4024   priocntl_enable = true;  // Enable changing priorities
4025   return 0;
4026 }
4027 
4028 #define IAPRI(x)        ((iaparms_t *)((x).pc_clparms))
4029 #define RTPRI(x)        ((rtparms_t *)((x).pc_clparms))
4030 #define TSPRI(x)        ((tsparms_t *)((x).pc_clparms))
4031 #define FXPRI(x)        ((fxparms_t *)((x).pc_clparms))
4032 
4033 
4034 // scale_to_lwp_priority
4035 //
4036 // Convert from the libthread "thr_setprio" scale to our current
4037 // lwp scheduling class scale.
4038 //
4039 static
4040 int     scale_to_lwp_priority (int rMin, int rMax, int x)
4041 {
4042   int v;
4043 
4044   if (x == 127) return rMax;            // avoid round-down
4045     v = (((x*(rMax-rMin)))/128)+rMin;
4046   return v;
4047 }
4048 
4049 
4050 // set_lwp_class_and_priority
4051 //
4052 // Set the class and priority of the lwp.  This call should only
4053 // be made when using bound threads (T2 threads are bound by default).
4054 //
4055 int set_lwp_class_and_priority(int ThreadID, int lwpid,
4056                                int newPrio, int new_class, bool scale) {
4057   int rslt;
4058   int Actual, Expected, prv;
4059   pcparms_t ParmInfo;                   // for GET-SET
4060 #ifdef ASSERT
4061   pcparms_t ReadBack;                   // for readback
4062 #endif
4063 
4064   // Set priority via PC_GETPARMS, update, PC_SETPARMS
4065   // Query current values.
4066   // TODO: accelerate this by eliminating the PC_GETPARMS call.
4067   // Cache "pcparms_t" in global ParmCache.
4068   // TODO: elide set-to-same-value
4069 
4070   // If something went wrong on init, don't change priorities.
4071   if ( !priocntl_enable ) {
4072     if (ThreadPriorityVerbose)
4073       tty->print_cr("Trying to set priority but init failed, ignoring");
4074     return EINVAL;
4075   }
4076 
4077   // If lwp hasn't started yet, just return
4078   // the _start routine will call us again.
4079   if ( lwpid <= 0 ) {
4080     if (ThreadPriorityVerbose) {
4081       tty->print_cr ("deferring the set_lwp_class_and_priority of thread "
4082                      INTPTR_FORMAT " to %d, lwpid not set",
4083                      ThreadID, newPrio);
4084     }
4085     return 0;
4086   }
4087 
4088   if (ThreadPriorityVerbose) {
4089     tty->print_cr ("set_lwp_class_and_priority("
4090                    INTPTR_FORMAT "@" INTPTR_FORMAT " %d) ",
4091                    ThreadID, lwpid, newPrio);
4092   }
4093 
4094   memset(&ParmInfo, 0, sizeof(pcparms_t));
4095   ParmInfo.pc_cid = PC_CLNULL;
4096   rslt = (*priocntl_ptr)(PC_VERSION, P_LWPID, lwpid, PC_GETPARMS, (caddr_t)&ParmInfo);
4097   if (rslt < 0) return errno;
4098 
4099   int cur_class = ParmInfo.pc_cid;
4100   ParmInfo.pc_cid = (id_t)new_class;
4101 
4102   if (new_class == rtLimits.schedPolicy) {
4103     rtparms_t *rtInfo  = (rtparms_t*)ParmInfo.pc_clparms;
4104     rtInfo->rt_pri     = scale ? scale_to_lwp_priority(rtLimits.minPrio,
4105                                                        rtLimits.maxPrio, newPrio)
4106                                : newPrio;
4107     rtInfo->rt_tqsecs  = RT_NOCHANGE;
4108     rtInfo->rt_tqnsecs = RT_NOCHANGE;
4109     if (ThreadPriorityVerbose) {
4110       tty->print_cr("RT: %d->%d\n", newPrio, rtInfo->rt_pri);
4111     }
4112   } else if (new_class == iaLimits.schedPolicy) {
4113     iaparms_t* iaInfo  = (iaparms_t*)ParmInfo.pc_clparms;
4114     int maxClamped     = MIN2(iaLimits.maxPrio,
4115                               cur_class == new_class
4116                                 ? (int)iaInfo->ia_uprilim : iaLimits.maxPrio);
4117     iaInfo->ia_upri    = scale ? scale_to_lwp_priority(iaLimits.minPrio,
4118                                                        maxClamped, newPrio)
4119                                : newPrio;
4120     iaInfo->ia_uprilim = cur_class == new_class
4121                            ? IA_NOCHANGE : (pri_t)iaLimits.maxPrio;
4122     iaInfo->ia_mode    = IA_NOCHANGE;
4123     if (ThreadPriorityVerbose) {
4124       tty->print_cr("IA: [%d...%d] %d->%d\n",
4125                     iaLimits.minPrio, maxClamped, newPrio, iaInfo->ia_upri);
4126     }
4127   } else if (new_class == tsLimits.schedPolicy) {
4128     tsparms_t* tsInfo  = (tsparms_t*)ParmInfo.pc_clparms;
4129     int maxClamped     = MIN2(tsLimits.maxPrio,
4130                               cur_class == new_class
4131                                 ? (int)tsInfo->ts_uprilim : tsLimits.maxPrio);
4132     tsInfo->ts_upri    = scale ? scale_to_lwp_priority(tsLimits.minPrio,
4133                                                        maxClamped, newPrio)
4134                                : newPrio;
4135     tsInfo->ts_uprilim = cur_class == new_class
4136                            ? TS_NOCHANGE : (pri_t)tsLimits.maxPrio;
4137     if (ThreadPriorityVerbose) {
4138       tty->print_cr("TS: [%d...%d] %d->%d\n",
4139                     tsLimits.minPrio, maxClamped, newPrio, tsInfo->ts_upri);
4140     }
4141   } else if (new_class == fxLimits.schedPolicy) {
4142     fxparms_t* fxInfo  = (fxparms_t*)ParmInfo.pc_clparms;
4143     int maxClamped     = MIN2(fxLimits.maxPrio,
4144                               cur_class == new_class
4145                                 ? (int)fxInfo->fx_uprilim : fxLimits.maxPrio);
4146     fxInfo->fx_upri    = scale ? scale_to_lwp_priority(fxLimits.minPrio,
4147                                                        maxClamped, newPrio)
4148                                : newPrio;
4149     fxInfo->fx_uprilim = cur_class == new_class
4150                            ? FX_NOCHANGE : (pri_t)fxLimits.maxPrio;
4151     fxInfo->fx_tqsecs  = FX_NOCHANGE;
4152     fxInfo->fx_tqnsecs = FX_NOCHANGE;
4153     if (ThreadPriorityVerbose) {
4154       tty->print_cr("FX: [%d...%d] %d->%d\n",
4155                     fxLimits.minPrio, maxClamped, newPrio, fxInfo->fx_upri);
4156     }
4157   } else {
4158     if (ThreadPriorityVerbose) {
4159       tty->print_cr("Unknown new scheduling class %d\n", new_class);
4160     }
4161     return EINVAL;    // no clue, punt
4162   }
4163 
4164   rslt = (*priocntl_ptr)(PC_VERSION, P_LWPID, lwpid, PC_SETPARMS, (caddr_t)&ParmInfo);
4165   if (ThreadPriorityVerbose && rslt) {
4166     tty->print_cr ("PC_SETPARMS ->%d %d\n", rslt, errno);
4167   }
4168   if (rslt < 0) return errno;
4169 
4170 #ifdef ASSERT
4171   // Sanity check: read back what we just attempted to set.
4172   // In theory it could have changed in the interim ...
4173   //
4174   // The priocntl system call is tricky.
4175   // Sometimes it'll validate the priority value argument and
4176   // return EINVAL if unhappy.  At other times it fails silently.
4177   // Readbacks are prudent.
4178 
4179   if (!ReadBackValidate) return 0;
4180 
4181   memset(&ReadBack, 0, sizeof(pcparms_t));
4182   ReadBack.pc_cid = PC_CLNULL;
4183   rslt = (*priocntl_ptr)(PC_VERSION, P_LWPID, lwpid, PC_GETPARMS, (caddr_t)&ReadBack);
4184   assert(rslt >= 0, "priocntl failed");
4185   Actual = Expected = 0xBAD;
4186   assert(ParmInfo.pc_cid == ReadBack.pc_cid, "cid's don't match");
4187   if (ParmInfo.pc_cid == rtLimits.schedPolicy) {
4188     Actual   = RTPRI(ReadBack)->rt_pri;
4189     Expected = RTPRI(ParmInfo)->rt_pri;
4190   } else if (ParmInfo.pc_cid == iaLimits.schedPolicy) {
4191     Actual   = IAPRI(ReadBack)->ia_upri;
4192     Expected = IAPRI(ParmInfo)->ia_upri;
4193   } else if (ParmInfo.pc_cid == tsLimits.schedPolicy) {
4194     Actual   = TSPRI(ReadBack)->ts_upri;
4195     Expected = TSPRI(ParmInfo)->ts_upri;
4196   } else if (ParmInfo.pc_cid == fxLimits.schedPolicy) {
4197     Actual   = FXPRI(ReadBack)->fx_upri;
4198     Expected = FXPRI(ParmInfo)->fx_upri;
4199   } else {
4200     if (ThreadPriorityVerbose) {
4201       tty->print_cr("set_lwp_class_and_priority: unexpected class in readback: %d\n",
4202                     ParmInfo.pc_cid);
4203     }
4204   }
4205 
4206   if (Actual != Expected) {
4207     if (ThreadPriorityVerbose) {
4208       tty->print_cr ("set_lwp_class_and_priority(%d %d) Class=%d: actual=%d vs expected=%d\n",
4209                      lwpid, newPrio, ReadBack.pc_cid, Actual, Expected);
4210     }
4211   }
4212 #endif
4213 
4214   return 0;
4215 }
4216 
4217 // Solaris only gives access to 128 real priorities at a time,
4218 // so we expand Java's ten to fill this range.  This would be better
4219 // if we dynamically adjusted relative priorities.
4220 //
4221 // The ThreadPriorityPolicy option allows us to select 2 different
4222 // priority scales.
4223 //
4224 // ThreadPriorityPolicy=0
4225 // Since the Solaris' default priority is MaximumPriority, we do not
4226 // set a priority lower than Max unless a priority lower than
4227 // NormPriority is requested.
4228 //
4229 // ThreadPriorityPolicy=1
4230 // This mode causes the priority table to get filled with
4231 // linear values.  NormPriority get's mapped to 50% of the
4232 // Maximum priority an so on.  This will cause VM threads
4233 // to get unfair treatment against other Solaris processes
4234 // which do not explicitly alter their thread priorities.
4235 //
4236 
4237 int os::java_to_os_priority[CriticalPriority + 1] = {
4238   -99999,         // 0 Entry should never be used
4239 
4240   0,              // 1 MinPriority
4241   32,             // 2
4242   64,             // 3
4243 
4244   96,             // 4
4245   127,            // 5 NormPriority
4246   127,            // 6
4247 
4248   127,            // 7
4249   127,            // 8
4250   127,            // 9 NearMaxPriority
4251 
4252   127,            // 10 MaxPriority
4253 
4254   -criticalPrio   // 11 CriticalPriority
4255 };
4256 
4257 OSReturn os::set_native_priority(Thread* thread, int newpri) {
4258   OSThread* osthread = thread->osthread();
4259 
4260   // Save requested priority in case the thread hasn't been started
4261   osthread->set_native_priority(newpri);
4262 
4263   // Check for critical priority request
4264   bool fxcritical = false;
4265   if (newpri == -criticalPrio) {
4266     fxcritical = true;
4267     newpri = criticalPrio;
4268   }
4269 
4270   assert(newpri >= MinimumPriority && newpri <= MaximumPriority, "bad priority mapping");
4271   if (!UseThreadPriorities) return OS_OK;
4272 
4273   int status = 0;
4274 
4275   if (!fxcritical) {
4276     // Use thr_setprio only if we have a priority that thr_setprio understands
4277     status = thr_setprio(thread->osthread()->thread_id(), newpri);
4278   }
4279 
4280   if (os::Solaris::T2_libthread() ||
4281       (UseBoundThreads && osthread->is_vm_created())) {
4282     int lwp_status =
4283       set_lwp_class_and_priority(osthread->thread_id(),
4284                                  osthread->lwp_id(),
4285                                  newpri,
4286                                  fxcritical ? fxLimits.schedPolicy : myClass,
4287                                  !fxcritical);
4288     if (lwp_status != 0 && fxcritical) {
4289       // Try again, this time without changing the scheduling class
4290       newpri = java_MaxPriority_to_os_priority;
4291       lwp_status = set_lwp_class_and_priority(osthread->thread_id(),
4292                                               osthread->lwp_id(),
4293                                               newpri, myClass, false);
4294     }
4295     status |= lwp_status;
4296   }
4297   return (status == 0) ? OS_OK : OS_ERR;
4298 }
4299 
4300 
4301 OSReturn os::get_native_priority(const Thread* const thread, int *priority_ptr) {
4302   int p;
4303   if ( !UseThreadPriorities ) {
4304     *priority_ptr = NormalPriority;
4305     return OS_OK;
4306   }
4307   int status = thr_getprio(thread->osthread()->thread_id(), &p);
4308   if (status != 0) {
4309     return OS_ERR;
4310   }
4311   *priority_ptr = p;
4312   return OS_OK;
4313 }
4314 
4315 
4316 // Hint to the underlying OS that a task switch would not be good.
4317 // Void return because it's a hint and can fail.
4318 void os::hint_no_preempt() {
4319   schedctl_start(schedctl_init());
4320 }
4321 
4322 static void resume_clear_context(OSThread *osthread) {
4323   osthread->set_ucontext(NULL);
4324 }
4325 
4326 static void suspend_save_context(OSThread *osthread, ucontext_t* context) {
4327   osthread->set_ucontext(context);
4328 }
4329 
4330 static Semaphore sr_semaphore;
4331 
4332 void os::Solaris::SR_handler(Thread* thread, ucontext_t* uc) {
4333   // Save and restore errno to avoid confusing native code with EINTR
4334   // after sigsuspend.
4335   int old_errno = errno;
4336 
4337   OSThread* osthread = thread->osthread();
4338   assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread");
4339 
4340   os::SuspendResume::State current = osthread->sr.state();
4341   if (current == os::SuspendResume::SR_SUSPEND_REQUEST) {
4342     suspend_save_context(osthread, uc);
4343 
4344     // attempt to switch the state, we assume we had a SUSPEND_REQUEST
4345     os::SuspendResume::State state = osthread->sr.suspended();
4346     if (state == os::SuspendResume::SR_SUSPENDED) {
4347       sigset_t suspend_set;  // signals for sigsuspend()
4348 
4349       // get current set of blocked signals and unblock resume signal
4350       thr_sigsetmask(SIG_BLOCK, NULL, &suspend_set);
4351       sigdelset(&suspend_set, os::Solaris::SIGasync());
4352 
4353       sr_semaphore.signal();
4354       // wait here until we are resumed
4355       while (1) {
4356         sigsuspend(&suspend_set);
4357 
4358         os::SuspendResume::State result = osthread->sr.running();
4359         if (result == os::SuspendResume::SR_RUNNING) {
4360           sr_semaphore.signal();
4361           break;
4362         }
4363       }
4364 
4365     } else if (state == os::SuspendResume::SR_RUNNING) {
4366       // request was cancelled, continue
4367     } else {
4368       ShouldNotReachHere();
4369     }
4370 
4371     resume_clear_context(osthread);
4372   } else if (current == os::SuspendResume::SR_RUNNING) {
4373     // request was cancelled, continue
4374   } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {
4375     // ignore
4376   } else {
4377     // ignore
4378   }
4379 
4380   errno = old_errno;
4381 }
4382 
4383 
4384 void os::interrupt(Thread* thread) {
4385   assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer");
4386 
4387   OSThread* osthread = thread->osthread();
4388 
4389   int isInterrupted = osthread->interrupted();
4390   if (!isInterrupted) {
4391       osthread->set_interrupted(true);
4392       OrderAccess::fence();
4393       // os::sleep() is implemented with either poll (NULL,0,timeout) or
4394       // by parking on _SleepEvent.  If the former, thr_kill will unwedge
4395       // the sleeper by SIGINTR, otherwise the unpark() will wake the sleeper.
4396       ParkEvent * const slp = thread->_SleepEvent ;
4397       if (slp != NULL) slp->unpark() ;
4398   }
4399 
4400   // For JSR166:  unpark after setting status but before thr_kill -dl
4401   if (thread->is_Java_thread()) {
4402     ((JavaThread*)thread)->parker()->unpark();
4403   }
4404 
4405   // Handle interruptible wait() ...
4406   ParkEvent * const ev = thread->_ParkEvent ;
4407   if (ev != NULL) ev->unpark() ;
4408 
4409   // When events are used everywhere for os::sleep, then this thr_kill
4410   // will only be needed if UseVMInterruptibleIO is true.
4411 
4412   if (!isInterrupted) {
4413     int status = thr_kill(osthread->thread_id(), os::Solaris::SIGinterrupt());
4414     assert_status(status == 0, status, "thr_kill");
4415 
4416     // Bump thread interruption counter
4417     RuntimeService::record_thread_interrupt_signaled_count();
4418   }
4419 }
4420 
4421 
4422 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
4423   assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer");
4424 
4425   OSThread* osthread = thread->osthread();
4426 
4427   bool res = osthread->interrupted();
4428 
4429   // NOTE that since there is no "lock" around these two operations,
4430   // there is the possibility that the interrupted flag will be
4431   // "false" but that the interrupt event will be set. This is
4432   // intentional. The effect of this is that Object.wait() will appear
4433   // to have a spurious wakeup, which is not harmful, and the
4434   // possibility is so rare that it is not worth the added complexity
4435   // to add yet another lock. It has also been recommended not to put
4436   // the interrupted flag into the os::Solaris::Event structure,
4437   // because it hides the issue.
4438   if (res && clear_interrupted) {
4439     osthread->set_interrupted(false);
4440   }
4441   return res;
4442 }
4443 
4444 
4445 void os::print_statistics() {
4446 }
4447 
4448 int os::message_box(const char* title, const char* message) {
4449   int i;
4450   fdStream err(defaultStream::error_fd());
4451   for (i = 0; i < 78; i++) err.print_raw("=");
4452   err.cr();
4453   err.print_raw_cr(title);
4454   for (i = 0; i < 78; i++) err.print_raw("-");
4455   err.cr();
4456   err.print_raw_cr(message);
4457   for (i = 0; i < 78; i++) err.print_raw("=");
4458   err.cr();
4459 
4460   char buf[16];
4461   // Prevent process from exiting upon "read error" without consuming all CPU
4462   while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
4463 
4464   return buf[0] == 'y' || buf[0] == 'Y';
4465 }
4466 
4467 static int sr_notify(OSThread* osthread) {
4468   int status = thr_kill(osthread->thread_id(), os::Solaris::SIGasync());
4469   assert_status(status == 0, status, "thr_kill");
4470   return status;
4471 }
4472 
4473 // "Randomly" selected value for how long we want to spin
4474 // before bailing out on suspending a thread, also how often
4475 // we send a signal to a thread we want to resume
4476 static const int RANDOMLY_LARGE_INTEGER = 1000000;
4477 static const int RANDOMLY_LARGE_INTEGER2 = 100;
4478 
4479 static bool do_suspend(OSThread* osthread) {
4480   assert(osthread->sr.is_running(), "thread should be running");
4481   assert(!sr_semaphore.trywait(), "semaphore has invalid state");
4482 
4483   // mark as suspended and send signal
4484   if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) {
4485     // failed to switch, state wasn't running?
4486     ShouldNotReachHere();
4487     return false;
4488   }
4489 
4490   if (sr_notify(osthread) != 0) {
4491     ShouldNotReachHere();
4492   }
4493 
4494   // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED
4495   while (true) {
4496     if (sr_semaphore.timedwait(0, 2000 * NANOSECS_PER_MILLISEC)) {
4497       break;
4498     } else {
4499       // timeout
4500       os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();
4501       if (cancelled == os::SuspendResume::SR_RUNNING) {
4502         return false;
4503       } else if (cancelled == os::SuspendResume::SR_SUSPENDED) {
4504         // make sure that we consume the signal on the semaphore as well
4505         sr_semaphore.wait();
4506         break;
4507       } else {
4508         ShouldNotReachHere();
4509         return false;
4510       }
4511     }
4512   }
4513 
4514   guarantee(osthread->sr.is_suspended(), "Must be suspended");
4515   return true;
4516 }
4517 
4518 static void do_resume(OSThread* osthread) {
4519   assert(osthread->sr.is_suspended(), "thread should be suspended");
4520   assert(!sr_semaphore.trywait(), "invalid semaphore state");
4521 
4522   if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) {
4523     // failed to switch to WAKEUP_REQUEST
4524     ShouldNotReachHere();
4525     return;
4526   }
4527 
4528   while (true) {
4529     if (sr_notify(osthread) == 0) {
4530       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
4531         if (osthread->sr.is_running()) {
4532           return;
4533         }
4534       }
4535     } else {
4536       ShouldNotReachHere();
4537     }
4538   }
4539 
4540   guarantee(osthread->sr.is_running(), "Must be running!");
4541 }
4542 
4543 void os::SuspendedThreadTask::internal_do_task() {
4544   if (do_suspend(_thread->osthread())) {
4545     SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext());
4546     do_task(context);
4547     do_resume(_thread->osthread());
4548   }
4549 }
4550 
4551 class PcFetcher : public os::SuspendedThreadTask {
4552 public:
4553   PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {}
4554   ExtendedPC result();
4555 protected:
4556   void do_task(const os::SuspendedThreadTaskContext& context);
4557 private:
4558   ExtendedPC _epc;
4559 };
4560 
4561 ExtendedPC PcFetcher::result() {
4562   guarantee(is_done(), "task is not done yet.");
4563   return _epc;
4564 }
4565 
4566 void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) {
4567   Thread* thread = context.thread();
4568   OSThread* osthread = thread->osthread();
4569   if (osthread->ucontext() != NULL) {
4570     _epc = os::Solaris::ucontext_get_pc((ucontext_t *) context.ucontext());
4571   } else {
4572     // NULL context is unexpected, double-check this is the VMThread
4573     guarantee(thread->is_VM_thread(), "can only be called for VMThread");
4574   }
4575 }
4576 
4577 // A lightweight implementation that does not suspend the target thread and
4578 // thus returns only a hint. Used for profiling only!
4579 ExtendedPC os::get_thread_pc(Thread* thread) {
4580   // Make sure that it is called by the watcher and the Threads lock is owned.
4581   assert(Thread::current()->is_Watcher_thread(), "Must be watcher and own Threads_lock");
4582   // For now, is only used to profile the VM Thread
4583   assert(thread->is_VM_thread(), "Can only be called for VMThread");
4584   PcFetcher fetcher(thread);
4585   fetcher.run();
4586   return fetcher.result();
4587 }
4588 
4589 
4590 // This does not do anything on Solaris. This is basically a hook for being
4591 // able to use structured exception handling (thread-local exception filters) on, e.g., Win32.
4592 void os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method, JavaCallArguments* args, Thread* thread) {
4593   f(value, method, args, thread);
4594 }
4595 
4596 // This routine may be used by user applications as a "hook" to catch signals.
4597 // The user-defined signal handler must pass unrecognized signals to this
4598 // routine, and if it returns true (non-zero), then the signal handler must
4599 // return immediately.  If the flag "abort_if_unrecognized" is true, then this
4600 // routine will never retun false (zero), but instead will execute a VM panic
4601 // routine kill the process.
4602 //
4603 // If this routine returns false, it is OK to call it again.  This allows
4604 // the user-defined signal handler to perform checks either before or after
4605 // the VM performs its own checks.  Naturally, the user code would be making
4606 // a serious error if it tried to handle an exception (such as a null check
4607 // or breakpoint) that the VM was generating for its own correct operation.
4608 //
4609 // This routine may recognize any of the following kinds of signals:
4610 // SIGBUS, SIGSEGV, SIGILL, SIGFPE, BREAK_SIGNAL, SIGPIPE, SIGXFSZ,
4611 // os::Solaris::SIGasync
4612 // It should be consulted by handlers for any of those signals.
4613 // It explicitly does not recognize os::Solaris::SIGinterrupt
4614 //
4615 // The caller of this routine must pass in the three arguments supplied
4616 // to the function referred to in the "sa_sigaction" (not the "sa_handler")
4617 // field of the structure passed to sigaction().  This routine assumes that
4618 // the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART.
4619 //
4620 // Note that the VM will print warnings if it detects conflicting signal
4621 // handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers".
4622 //
4623 extern "C" JNIEXPORT int
4624 JVM_handle_solaris_signal(int signo, siginfo_t* siginfo, void* ucontext,
4625                           int abort_if_unrecognized);
4626 
4627 
4628 void signalHandler(int sig, siginfo_t* info, void* ucVoid) {
4629   JVM_handle_solaris_signal(sig, info, ucVoid, true);
4630 }
4631 
4632 /* Do not delete - if guarantee is ever removed,  a signal handler (even empty)
4633    is needed to provoke threads blocked on IO to return an EINTR
4634    Note: this explicitly does NOT call JVM_handle_solaris_signal and
4635    does NOT participate in signal chaining due to requirement for
4636    NOT setting SA_RESTART to make EINTR work. */
4637 extern "C" void sigINTRHandler(int sig, siginfo_t* info, void* ucVoid) {
4638    if (UseSignalChaining) {
4639       struct sigaction *actp = os::Solaris::get_chained_signal_action(sig);
4640       if (actp && actp->sa_handler) {
4641         vm_exit_during_initialization("Signal chaining detected for VM interrupt signal, try -XX:+UseAltSigs");
4642       }
4643    }
4644 }
4645 
4646 // This boolean allows users to forward their own non-matching signals
4647 // to JVM_handle_solaris_signal, harmlessly.
4648 bool os::Solaris::signal_handlers_are_installed = false;
4649 
4650 // For signal-chaining
4651 bool os::Solaris::libjsig_is_loaded = false;
4652 typedef struct sigaction *(*get_signal_t)(int);
4653 get_signal_t os::Solaris::get_signal_action = NULL;
4654 
4655 struct sigaction* os::Solaris::get_chained_signal_action(int sig) {
4656   struct sigaction *actp = NULL;
4657 
4658   if ((libjsig_is_loaded)  && (sig <= Maxlibjsigsigs)) {
4659     // Retrieve the old signal handler from libjsig
4660     actp = (*get_signal_action)(sig);
4661   }
4662   if (actp == NULL) {
4663     // Retrieve the preinstalled signal handler from jvm
4664     actp = get_preinstalled_handler(sig);
4665   }
4666 
4667   return actp;
4668 }
4669 
4670 static bool call_chained_handler(struct sigaction *actp, int sig,
4671                                  siginfo_t *siginfo, void *context) {
4672   // Call the old signal handler
4673   if (actp->sa_handler == SIG_DFL) {
4674     // It's more reasonable to let jvm treat it as an unexpected exception
4675     // instead of taking the default action.
4676     return false;
4677   } else if (actp->sa_handler != SIG_IGN) {
4678     if ((actp->sa_flags & SA_NODEFER) == 0) {
4679       // automaticlly block the signal
4680       sigaddset(&(actp->sa_mask), sig);
4681     }
4682 
4683     sa_handler_t hand;
4684     sa_sigaction_t sa;
4685     bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0;
4686     // retrieve the chained handler
4687     if (siginfo_flag_set) {
4688       sa = actp->sa_sigaction;
4689     } else {
4690       hand = actp->sa_handler;
4691     }
4692 
4693     if ((actp->sa_flags & SA_RESETHAND) != 0) {
4694       actp->sa_handler = SIG_DFL;
4695     }
4696 
4697     // try to honor the signal mask
4698     sigset_t oset;
4699     thr_sigsetmask(SIG_SETMASK, &(actp->sa_mask), &oset);
4700 
4701     // call into the chained handler
4702     if (siginfo_flag_set) {
4703       (*sa)(sig, siginfo, context);
4704     } else {
4705       (*hand)(sig);
4706     }
4707 
4708     // restore the signal mask
4709     thr_sigsetmask(SIG_SETMASK, &oset, 0);
4710   }
4711   // Tell jvm's signal handler the signal is taken care of.
4712   return true;
4713 }
4714 
4715 bool os::Solaris::chained_handler(int sig, siginfo_t* siginfo, void* context) {
4716   bool chained = false;
4717   // signal-chaining
4718   if (UseSignalChaining) {
4719     struct sigaction *actp = get_chained_signal_action(sig);
4720     if (actp != NULL) {
4721       chained = call_chained_handler(actp, sig, siginfo, context);
4722     }
4723   }
4724   return chained;
4725 }
4726 
4727 struct sigaction* os::Solaris::get_preinstalled_handler(int sig) {
4728   assert((chainedsigactions != (struct sigaction *)NULL) && (preinstalled_sigs != (int *)NULL) , "signals not yet initialized");
4729   if (preinstalled_sigs[sig] != 0) {
4730     return &chainedsigactions[sig];
4731   }
4732   return NULL;
4733 }
4734 
4735 void os::Solaris::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
4736 
4737   assert(sig > 0 && sig <= Maxsignum, "vm signal out of expected range");
4738   assert((chainedsigactions != (struct sigaction *)NULL) && (preinstalled_sigs != (int *)NULL) , "signals not yet initialized");
4739   chainedsigactions[sig] = oldAct;
4740   preinstalled_sigs[sig] = 1;
4741 }
4742 
4743 void os::Solaris::set_signal_handler(int sig, bool set_installed, bool oktochain) {
4744   // Check for overwrite.
4745   struct sigaction oldAct;
4746   sigaction(sig, (struct sigaction*)NULL, &oldAct);
4747   void* oldhand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oldAct.sa_sigaction)
4748                                       : CAST_FROM_FN_PTR(void*,  oldAct.sa_handler);
4749   if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
4750       oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
4751       oldhand != CAST_FROM_FN_PTR(void*, signalHandler)) {
4752     if (AllowUserSignalHandlers || !set_installed) {
4753       // Do not overwrite; user takes responsibility to forward to us.
4754       return;
4755     } else if (UseSignalChaining) {
4756       if (oktochain) {
4757         // save the old handler in jvm
4758         save_preinstalled_handler(sig, oldAct);
4759       } else {
4760         vm_exit_during_initialization("Signal chaining not allowed for VM interrupt signal, try -XX:+UseAltSigs.");
4761       }
4762       // libjsig also interposes the sigaction() call below and saves the
4763       // old sigaction on it own.
4764     } else {
4765       fatal(err_msg("Encountered unexpected pre-existing sigaction handler "
4766                     "%#lx for signal %d.", (long)oldhand, sig));
4767     }
4768   }
4769 
4770   struct sigaction sigAct;
4771   sigfillset(&(sigAct.sa_mask));
4772   sigAct.sa_handler = SIG_DFL;
4773 
4774   sigAct.sa_sigaction = signalHandler;
4775   // Handle SIGSEGV on alternate signal stack if
4776   // not using stack banging
4777   if (!UseStackBanging && sig == SIGSEGV) {
4778     sigAct.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
4779   // Interruptible i/o requires SA_RESTART cleared so EINTR
4780   // is returned instead of restarting system calls
4781   } else if (sig == os::Solaris::SIGinterrupt()) {
4782     sigemptyset(&sigAct.sa_mask);
4783     sigAct.sa_handler = NULL;
4784     sigAct.sa_flags = SA_SIGINFO;
4785     sigAct.sa_sigaction = sigINTRHandler;
4786   } else {
4787     sigAct.sa_flags = SA_SIGINFO | SA_RESTART;
4788   }
4789   os::Solaris::set_our_sigflags(sig, sigAct.sa_flags);
4790 
4791   sigaction(sig, &sigAct, &oldAct);
4792 
4793   void* oldhand2 = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
4794                                        : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
4795   assert(oldhand2 == oldhand, "no concurrent signal handler installation");
4796 }
4797 
4798 
4799 #define DO_SIGNAL_CHECK(sig) \
4800   if (!sigismember(&check_signal_done, sig)) \
4801     os::Solaris::check_signal_handler(sig)
4802 
4803 // This method is a periodic task to check for misbehaving JNI applications
4804 // under CheckJNI, we can add any periodic checks here
4805 
4806 void os::run_periodic_checks() {
4807   // A big source of grief is hijacking virt. addr 0x0 on Solaris,
4808   // thereby preventing a NULL checks.
4809   if(!check_addr0_done) check_addr0_done = check_addr0(tty);
4810 
4811   if (check_signals == false) return;
4812 
4813   // SEGV and BUS if overridden could potentially prevent
4814   // generation of hs*.log in the event of a crash, debugging
4815   // such a case can be very challenging, so we absolutely
4816   // check for the following for a good measure:
4817   DO_SIGNAL_CHECK(SIGSEGV);
4818   DO_SIGNAL_CHECK(SIGILL);
4819   DO_SIGNAL_CHECK(SIGFPE);
4820   DO_SIGNAL_CHECK(SIGBUS);
4821   DO_SIGNAL_CHECK(SIGPIPE);
4822   DO_SIGNAL_CHECK(SIGXFSZ);
4823 
4824   // ReduceSignalUsage allows the user to override these handlers
4825   // see comments at the very top and jvm_solaris.h
4826   if (!ReduceSignalUsage) {
4827     DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL);
4828     DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL);
4829     DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL);
4830     DO_SIGNAL_CHECK(BREAK_SIGNAL);
4831   }
4832 
4833   // See comments above for using JVM1/JVM2 and UseAltSigs
4834   DO_SIGNAL_CHECK(os::Solaris::SIGinterrupt());
4835   DO_SIGNAL_CHECK(os::Solaris::SIGasync());
4836 
4837 }
4838 
4839 typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);
4840 
4841 static os_sigaction_t os_sigaction = NULL;
4842 
4843 void os::Solaris::check_signal_handler(int sig) {
4844   char buf[O_BUFLEN];
4845   address jvmHandler = NULL;
4846 
4847   struct sigaction act;
4848   if (os_sigaction == NULL) {
4849     // only trust the default sigaction, in case it has been interposed
4850     os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction");
4851     if (os_sigaction == NULL) return;
4852   }
4853 
4854   os_sigaction(sig, (struct sigaction*)NULL, &act);
4855 
4856   address thisHandler = (act.sa_flags & SA_SIGINFO)
4857     ? CAST_FROM_FN_PTR(address, act.sa_sigaction)
4858     : CAST_FROM_FN_PTR(address, act.sa_handler) ;
4859 
4860 
4861   switch(sig) {
4862     case SIGSEGV:
4863     case SIGBUS:
4864     case SIGFPE:
4865     case SIGPIPE:
4866     case SIGXFSZ:
4867     case SIGILL:
4868       jvmHandler = CAST_FROM_FN_PTR(address, signalHandler);
4869       break;
4870 
4871     case SHUTDOWN1_SIGNAL:
4872     case SHUTDOWN2_SIGNAL:
4873     case SHUTDOWN3_SIGNAL:
4874     case BREAK_SIGNAL:
4875       jvmHandler = (address)user_handler();
4876       break;
4877 
4878     default:
4879       int intrsig = os::Solaris::SIGinterrupt();
4880       int asynsig = os::Solaris::SIGasync();
4881 
4882       if (sig == intrsig) {
4883         jvmHandler = CAST_FROM_FN_PTR(address, sigINTRHandler);
4884       } else if (sig == asynsig) {
4885         jvmHandler = CAST_FROM_FN_PTR(address, signalHandler);
4886       } else {
4887         return;
4888       }
4889       break;
4890   }
4891 
4892 
4893   if (thisHandler != jvmHandler) {
4894     tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN));
4895     tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN));
4896     tty->print_cr("  found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN));
4897     // No need to check this sig any longer
4898     sigaddset(&check_signal_done, sig);
4899   } else if(os::Solaris::get_our_sigflags(sig) != 0 && act.sa_flags != os::Solaris::get_our_sigflags(sig)) {
4900     tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN));
4901     tty->print("expected:" PTR32_FORMAT, os::Solaris::get_our_sigflags(sig));
4902     tty->print_cr("  found:" PTR32_FORMAT, act.sa_flags);
4903     // No need to check this sig any longer
4904     sigaddset(&check_signal_done, sig);
4905   }
4906 
4907   // Print all the signal handler state
4908   if (sigismember(&check_signal_done, sig)) {
4909     print_signal_handlers(tty, buf, O_BUFLEN);
4910   }
4911 
4912 }
4913 
4914 void os::Solaris::install_signal_handlers() {
4915   bool libjsigdone = false;
4916   signal_handlers_are_installed = true;
4917 
4918   // signal-chaining
4919   typedef void (*signal_setting_t)();
4920   signal_setting_t begin_signal_setting = NULL;
4921   signal_setting_t end_signal_setting = NULL;
4922   begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
4923                                         dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting"));
4924   if (begin_signal_setting != NULL) {
4925     end_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
4926                                         dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));
4927     get_signal_action = CAST_TO_FN_PTR(get_signal_t,
4928                                        dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));
4929     get_libjsig_version = CAST_TO_FN_PTR(version_getting_t,
4930                                          dlsym(RTLD_DEFAULT, "JVM_get_libjsig_version"));
4931     libjsig_is_loaded = true;
4932     if (os::Solaris::get_libjsig_version != NULL) {
4933       libjsigversion =  (*os::Solaris::get_libjsig_version)();
4934     }
4935     assert(UseSignalChaining, "should enable signal-chaining");
4936   }
4937   if (libjsig_is_loaded) {
4938     // Tell libjsig jvm is setting signal handlers
4939     (*begin_signal_setting)();
4940   }
4941 
4942   set_signal_handler(SIGSEGV, true, true);
4943   set_signal_handler(SIGPIPE, true, true);
4944   set_signal_handler(SIGXFSZ, true, true);
4945   set_signal_handler(SIGBUS, true, true);
4946   set_signal_handler(SIGILL, true, true);
4947   set_signal_handler(SIGFPE, true, true);
4948 
4949 
4950   if (os::Solaris::SIGinterrupt() > OLDMAXSIGNUM || os::Solaris::SIGasync() > OLDMAXSIGNUM) {
4951 
4952     // Pre-1.4.1 Libjsig limited to signal chaining signals <= 32 so
4953     // can not register overridable signals which might be > 32
4954     if (libjsig_is_loaded && libjsigversion <= JSIG_VERSION_1_4_1) {
4955     // Tell libjsig jvm has finished setting signal handlers
4956       (*end_signal_setting)();
4957       libjsigdone = true;
4958     }
4959   }
4960 
4961   // Never ok to chain our SIGinterrupt
4962   set_signal_handler(os::Solaris::SIGinterrupt(), true, false);
4963   set_signal_handler(os::Solaris::SIGasync(), true, true);
4964 
4965   if (libjsig_is_loaded && !libjsigdone) {
4966     // Tell libjsig jvm finishes setting signal handlers
4967     (*end_signal_setting)();
4968   }
4969 
4970   // We don't activate signal checker if libjsig is in place, we trust ourselves
4971   // and if UserSignalHandler is installed all bets are off.
4972   // Log that signal checking is off only if -verbose:jni is specified.
4973   if (CheckJNICalls) {
4974     if (libjsig_is_loaded) {
4975       if (PrintJNIResolving) {
4976         tty->print_cr("Info: libjsig is activated, all active signal checking is disabled");
4977       }
4978       check_signals = false;
4979     }
4980     if (AllowUserSignalHandlers) {
4981       if (PrintJNIResolving) {
4982         tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
4983       }
4984       check_signals = false;
4985     }
4986   }
4987 }
4988 
4989 
4990 void report_error(const char* file_name, int line_no, const char* title, const char* format, ...);
4991 
4992 const char * signames[] = {
4993   "SIG0",
4994   "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP",
4995   "SIGABRT", "SIGEMT", "SIGFPE", "SIGKILL", "SIGBUS",
4996   "SIGSEGV", "SIGSYS", "SIGPIPE", "SIGALRM", "SIGTERM",
4997   "SIGUSR1", "SIGUSR2", "SIGCLD", "SIGPWR", "SIGWINCH",
4998   "SIGURG", "SIGPOLL", "SIGSTOP", "SIGTSTP", "SIGCONT",
4999   "SIGTTIN", "SIGTTOU", "SIGVTALRM", "SIGPROF", "SIGXCPU",
5000   "SIGXFSZ", "SIGWAITING", "SIGLWP", "SIGFREEZE", "SIGTHAW",
5001   "SIGCANCEL", "SIGLOST"
5002 };
5003 
5004 const char* os::exception_name(int exception_code, char* buf, size_t size) {
5005   if (0 < exception_code && exception_code <= SIGRTMAX) {
5006     // signal
5007     if (exception_code < sizeof(signames)/sizeof(const char*)) {
5008        jio_snprintf(buf, size, "%s", signames[exception_code]);
5009     } else {
5010        jio_snprintf(buf, size, "SIG%d", exception_code);
5011     }
5012     return buf;
5013   } else {
5014     return NULL;
5015   }
5016 }
5017 
5018 // (Static) wrappers for the new libthread API
5019 int_fnP_thread_t_iP_uP_stack_tP_gregset_t os::Solaris::_thr_getstate;
5020 int_fnP_thread_t_i_gregset_t os::Solaris::_thr_setstate;
5021 int_fnP_thread_t_i os::Solaris::_thr_setmutator;
5022 int_fnP_thread_t os::Solaris::_thr_suspend_mutator;
5023 int_fnP_thread_t os::Solaris::_thr_continue_mutator;
5024 
5025 // (Static) wrapper for getisax(2) call.
5026 os::Solaris::getisax_func_t os::Solaris::_getisax = 0;
5027 
5028 // (Static) wrappers for the liblgrp API
5029 os::Solaris::lgrp_home_func_t os::Solaris::_lgrp_home;
5030 os::Solaris::lgrp_init_func_t os::Solaris::_lgrp_init;
5031 os::Solaris::lgrp_fini_func_t os::Solaris::_lgrp_fini;
5032 os::Solaris::lgrp_root_func_t os::Solaris::_lgrp_root;
5033 os::Solaris::lgrp_children_func_t os::Solaris::_lgrp_children;
5034 os::Solaris::lgrp_resources_func_t os::Solaris::_lgrp_resources;
5035 os::Solaris::lgrp_nlgrps_func_t os::Solaris::_lgrp_nlgrps;
5036 os::Solaris::lgrp_cookie_stale_func_t os::Solaris::_lgrp_cookie_stale;
5037 os::Solaris::lgrp_cookie_t os::Solaris::_lgrp_cookie = 0;
5038 
5039 // (Static) wrapper for meminfo() call.
5040 os::Solaris::meminfo_func_t os::Solaris::_meminfo = 0;
5041 
5042 static address resolve_symbol_lazy(const char* name) {
5043   address addr = (address) dlsym(RTLD_DEFAULT, name);
5044   if(addr == NULL) {
5045     // RTLD_DEFAULT was not defined on some early versions of 2.5.1
5046     addr = (address) dlsym(RTLD_NEXT, name);
5047   }
5048   return addr;
5049 }
5050 
5051 static address resolve_symbol(const char* name) {
5052   address addr = resolve_symbol_lazy(name);
5053   if(addr == NULL) {
5054     fatal(dlerror());
5055   }
5056   return addr;
5057 }
5058 
5059 
5060 
5061 // isT2_libthread()
5062 //
5063 // Routine to determine if we are currently using the new T2 libthread.
5064 //
5065 // We determine if we are using T2 by reading /proc/self/lstatus and
5066 // looking for a thread with the ASLWP bit set.  If we find this status
5067 // bit set, we must assume that we are NOT using T2.  The T2 team
5068 // has approved this algorithm.
5069 //
5070 // We need to determine if we are running with the new T2 libthread
5071 // since setting native thread priorities is handled differently
5072 // when using this library.  All threads created using T2 are bound
5073 // threads. Calling thr_setprio is meaningless in this case.
5074 //
5075 bool isT2_libthread() {
5076   static prheader_t * lwpArray = NULL;
5077   static int lwpSize = 0;
5078   static int lwpFile = -1;
5079   lwpstatus_t * that;
5080   char lwpName [128];
5081   bool isT2 = false;
5082 
5083 #define ADR(x)  ((uintptr_t)(x))
5084 #define LWPINDEX(ary,ix)   ((lwpstatus_t *)(((ary)->pr_entsize * (ix)) + (ADR((ary) + 1))))
5085 
5086   lwpFile = ::open("/proc/self/lstatus", O_RDONLY, 0);
5087   if (lwpFile < 0) {
5088       if (ThreadPriorityVerbose) warning ("Couldn't open /proc/self/lstatus\n");
5089       return false;
5090   }
5091   lwpSize = 16*1024;
5092   for (;;) {
5093     ::lseek64 (lwpFile, 0, SEEK_SET);
5094     lwpArray = (prheader_t *)NEW_C_HEAP_ARRAY(char, lwpSize, mtInternal);
5095     if (::read(lwpFile, lwpArray, lwpSize) < 0) {
5096       if (ThreadPriorityVerbose) warning("Error reading /proc/self/lstatus\n");
5097       break;
5098     }
5099     if ((lwpArray->pr_nent * lwpArray->pr_entsize) <= lwpSize) {
5100        // We got a good snapshot - now iterate over the list.
5101       int aslwpcount = 0;
5102       for (int i = 0; i < lwpArray->pr_nent; i++ ) {
5103         that = LWPINDEX(lwpArray,i);
5104         if (that->pr_flags & PR_ASLWP) {
5105           aslwpcount++;
5106         }
5107       }
5108       if (aslwpcount == 0) isT2 = true;
5109       break;
5110     }
5111     lwpSize = lwpArray->pr_nent * lwpArray->pr_entsize;
5112     FREE_C_HEAP_ARRAY(char, lwpArray, mtInternal);  // retry.
5113   }
5114 
5115   FREE_C_HEAP_ARRAY(char, lwpArray, mtInternal);
5116   ::close (lwpFile);
5117   if (ThreadPriorityVerbose) {
5118     if (isT2) tty->print_cr("We are running with a T2 libthread\n");
5119     else tty->print_cr("We are not running with a T2 libthread\n");
5120   }
5121   return isT2;
5122 }
5123 
5124 
5125 void os::Solaris::libthread_init() {
5126   address func = (address)dlsym(RTLD_DEFAULT, "_thr_suspend_allmutators");
5127 
5128   // Determine if we are running with the new T2 libthread
5129   os::Solaris::set_T2_libthread(isT2_libthread());
5130 
5131   lwp_priocntl_init();
5132 
5133   // RTLD_DEFAULT was not defined on some early versions of 5.5.1
5134   if(func == NULL) {
5135     func = (address) dlsym(RTLD_NEXT, "_thr_suspend_allmutators");
5136     // Guarantee that this VM is running on an new enough OS (5.6 or
5137     // later) that it will have a new enough libthread.so.
5138     guarantee(func != NULL, "libthread.so is too old.");
5139   }
5140 
5141   // Initialize the new libthread getstate API wrappers
5142   func = resolve_symbol("thr_getstate");
5143   os::Solaris::set_thr_getstate(CAST_TO_FN_PTR(int_fnP_thread_t_iP_uP_stack_tP_gregset_t, func));
5144 
5145   func = resolve_symbol("thr_setstate");
5146   os::Solaris::set_thr_setstate(CAST_TO_FN_PTR(int_fnP_thread_t_i_gregset_t, func));
5147 
5148   func = resolve_symbol("thr_setmutator");
5149   os::Solaris::set_thr_setmutator(CAST_TO_FN_PTR(int_fnP_thread_t_i, func));
5150 
5151   func = resolve_symbol("thr_suspend_mutator");
5152   os::Solaris::set_thr_suspend_mutator(CAST_TO_FN_PTR(int_fnP_thread_t, func));
5153 
5154   func = resolve_symbol("thr_continue_mutator");
5155   os::Solaris::set_thr_continue_mutator(CAST_TO_FN_PTR(int_fnP_thread_t, func));
5156 
5157   int size;
5158   void (*handler_info_func)(address *, int *);
5159   handler_info_func = CAST_TO_FN_PTR(void (*)(address *, int *), resolve_symbol("thr_sighndlrinfo"));
5160   handler_info_func(&handler_start, &size);
5161   handler_end = handler_start + size;
5162 }
5163 
5164 
5165 int_fnP_mutex_tP os::Solaris::_mutex_lock;
5166 int_fnP_mutex_tP os::Solaris::_mutex_trylock;
5167 int_fnP_mutex_tP os::Solaris::_mutex_unlock;
5168 int_fnP_mutex_tP_i_vP os::Solaris::_mutex_init;
5169 int_fnP_mutex_tP os::Solaris::_mutex_destroy;
5170 int os::Solaris::_mutex_scope = USYNC_THREAD;
5171 
5172 int_fnP_cond_tP_mutex_tP_timestruc_tP os::Solaris::_cond_timedwait;
5173 int_fnP_cond_tP_mutex_tP os::Solaris::_cond_wait;
5174 int_fnP_cond_tP os::Solaris::_cond_signal;
5175 int_fnP_cond_tP os::Solaris::_cond_broadcast;
5176 int_fnP_cond_tP_i_vP os::Solaris::_cond_init;
5177 int_fnP_cond_tP os::Solaris::_cond_destroy;
5178 int os::Solaris::_cond_scope = USYNC_THREAD;
5179 
5180 void os::Solaris::synchronization_init() {
5181   if(UseLWPSynchronization) {
5182     os::Solaris::set_mutex_lock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("_lwp_mutex_lock")));
5183     os::Solaris::set_mutex_trylock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("_lwp_mutex_trylock")));
5184     os::Solaris::set_mutex_unlock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("_lwp_mutex_unlock")));
5185     os::Solaris::set_mutex_init(lwp_mutex_init);
5186     os::Solaris::set_mutex_destroy(lwp_mutex_destroy);
5187     os::Solaris::set_mutex_scope(USYNC_THREAD);
5188 
5189     os::Solaris::set_cond_timedwait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP_timestruc_tP, resolve_symbol("_lwp_cond_timedwait")));
5190     os::Solaris::set_cond_wait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP, resolve_symbol("_lwp_cond_wait")));
5191     os::Solaris::set_cond_signal(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("_lwp_cond_signal")));
5192     os::Solaris::set_cond_broadcast(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("_lwp_cond_broadcast")));
5193     os::Solaris::set_cond_init(lwp_cond_init);
5194     os::Solaris::set_cond_destroy(lwp_cond_destroy);
5195     os::Solaris::set_cond_scope(USYNC_THREAD);
5196   }
5197   else {
5198     os::Solaris::set_mutex_scope(USYNC_THREAD);
5199     os::Solaris::set_cond_scope(USYNC_THREAD);
5200 
5201     if(UsePthreads) {
5202       os::Solaris::set_mutex_lock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_lock")));
5203       os::Solaris::set_mutex_trylock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_trylock")));
5204       os::Solaris::set_mutex_unlock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_unlock")));
5205       os::Solaris::set_mutex_init(pthread_mutex_default_init);
5206       os::Solaris::set_mutex_destroy(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("pthread_mutex_destroy")));
5207 
5208       os::Solaris::set_cond_timedwait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP_timestruc_tP, resolve_symbol("pthread_cond_timedwait")));
5209       os::Solaris::set_cond_wait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP, resolve_symbol("pthread_cond_wait")));
5210       os::Solaris::set_cond_signal(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("pthread_cond_signal")));
5211       os::Solaris::set_cond_broadcast(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("pthread_cond_broadcast")));
5212       os::Solaris::set_cond_init(pthread_cond_default_init);
5213       os::Solaris::set_cond_destroy(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("pthread_cond_destroy")));
5214     }
5215     else {
5216       os::Solaris::set_mutex_lock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("mutex_lock")));
5217       os::Solaris::set_mutex_trylock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("mutex_trylock")));
5218       os::Solaris::set_mutex_unlock(CAST_TO_FN_PTR(int_fnP_mutex_tP, resolve_symbol("mutex_unlock")));
5219       os::Solaris::set_mutex_init(::mutex_init);
5220       os::Solaris::set_mutex_destroy(::mutex_destroy);
5221 
5222       os::Solaris::set_cond_timedwait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP_timestruc_tP, resolve_symbol("cond_timedwait")));
5223       os::Solaris::set_cond_wait(CAST_TO_FN_PTR(int_fnP_cond_tP_mutex_tP, resolve_symbol("cond_wait")));
5224       os::Solaris::set_cond_signal(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("cond_signal")));
5225       os::Solaris::set_cond_broadcast(CAST_TO_FN_PTR(int_fnP_cond_tP, resolve_symbol("cond_broadcast")));
5226       os::Solaris::set_cond_init(::cond_init);
5227       os::Solaris::set_cond_destroy(::cond_destroy);
5228     }
5229   }
5230 }
5231 
5232 bool os::Solaris::liblgrp_init() {
5233   void *handle = dlopen("liblgrp.so.1", RTLD_LAZY);
5234   if (handle != NULL) {
5235     os::Solaris::set_lgrp_home(CAST_TO_FN_PTR(lgrp_home_func_t, dlsym(handle, "lgrp_home")));
5236     os::Solaris::set_lgrp_init(CAST_TO_FN_PTR(lgrp_init_func_t, dlsym(handle, "lgrp_init")));
5237     os::Solaris::set_lgrp_fini(CAST_TO_FN_PTR(lgrp_fini_func_t, dlsym(handle, "lgrp_fini")));
5238     os::Solaris::set_lgrp_root(CAST_TO_FN_PTR(lgrp_root_func_t, dlsym(handle, "lgrp_root")));
5239     os::Solaris::set_lgrp_children(CAST_TO_FN_PTR(lgrp_children_func_t, dlsym(handle, "lgrp_children")));
5240     os::Solaris::set_lgrp_resources(CAST_TO_FN_PTR(lgrp_resources_func_t, dlsym(handle, "lgrp_resources")));
5241     os::Solaris::set_lgrp_nlgrps(CAST_TO_FN_PTR(lgrp_nlgrps_func_t, dlsym(handle, "lgrp_nlgrps")));
5242     os::Solaris::set_lgrp_cookie_stale(CAST_TO_FN_PTR(lgrp_cookie_stale_func_t,
5243                                        dlsym(handle, "lgrp_cookie_stale")));
5244 
5245     lgrp_cookie_t c = lgrp_init(LGRP_VIEW_CALLER);
5246     set_lgrp_cookie(c);
5247     return true;
5248   }
5249   return false;
5250 }
5251 
5252 void os::Solaris::misc_sym_init() {
5253   address func;
5254 
5255   // getisax
5256   func = resolve_symbol_lazy("getisax");
5257   if (func != NULL) {
5258     os::Solaris::_getisax = CAST_TO_FN_PTR(getisax_func_t, func);
5259   }
5260 
5261   // meminfo
5262   func = resolve_symbol_lazy("meminfo");
5263   if (func != NULL) {
5264     os::Solaris::set_meminfo(CAST_TO_FN_PTR(meminfo_func_t, func));
5265   }
5266 }
5267 
5268 uint_t os::Solaris::getisax(uint32_t* array, uint_t n) {
5269   assert(_getisax != NULL, "_getisax not set");
5270   return _getisax(array, n);
5271 }
5272 
5273 // Symbol doesn't exist in Solaris 8 pset.h
5274 #ifndef PS_MYID
5275 #define PS_MYID -3
5276 #endif
5277 
5278 // int pset_getloadavg(psetid_t pset, double loadavg[], int nelem);
5279 typedef long (*pset_getloadavg_type)(psetid_t pset, double loadavg[], int nelem);
5280 static pset_getloadavg_type pset_getloadavg_ptr = NULL;
5281 
5282 void init_pset_getloadavg_ptr(void) {
5283   pset_getloadavg_ptr =
5284     (pset_getloadavg_type)dlsym(RTLD_DEFAULT, "pset_getloadavg");
5285   if (PrintMiscellaneous && Verbose && pset_getloadavg_ptr == NULL) {
5286     warning("pset_getloadavg function not found");
5287   }
5288 }
5289 
5290 int os::Solaris::_dev_zero_fd = -1;
5291 
5292 // this is called _before_ the global arguments have been parsed
5293 void os::init(void) {
5294   _initial_pid = getpid();
5295 
5296   max_hrtime = first_hrtime = gethrtime();
5297 
5298   init_random(1234567);
5299 
5300   page_size = sysconf(_SC_PAGESIZE);
5301   if (page_size == -1)
5302     fatal(err_msg("os_solaris.cpp: os::init: sysconf failed (%s)",
5303                   strerror(errno)));
5304   init_page_sizes((size_t) page_size);
5305 
5306   Solaris::initialize_system_info();
5307 
5308   // Initialize misc. symbols as soon as possible, so we can use them
5309   // if we need them.
5310   Solaris::misc_sym_init();
5311 
5312   int fd = ::open("/dev/zero", O_RDWR);
5313   if (fd < 0) {
5314     fatal(err_msg("os::init: cannot open /dev/zero (%s)", strerror(errno)));
5315   } else {
5316     Solaris::set_dev_zero_fd(fd);
5317 
5318     // Close on exec, child won't inherit.
5319     fcntl(fd, F_SETFD, FD_CLOEXEC);
5320   }
5321 
5322   clock_tics_per_sec = CLK_TCK;
5323 
5324   // check if dladdr1() exists; dladdr1 can provide more information than
5325   // dladdr for os::dll_address_to_function_name. It comes with SunOS 5.9
5326   // and is available on linker patches for 5.7 and 5.8.
5327   // libdl.so must have been loaded, this call is just an entry lookup
5328   void * hdl = dlopen("libdl.so", RTLD_NOW);
5329   if (hdl)
5330     dladdr1_func = CAST_TO_FN_PTR(dladdr1_func_type, dlsym(hdl, "dladdr1"));
5331 
5332   // (Solaris only) this switches to calls that actually do locking.
5333   ThreadCritical::initialize();
5334 
5335   main_thread = thr_self();
5336 
5337   // Constant minimum stack size allowed. It must be at least
5338   // the minimum of what the OS supports (thr_min_stack()), and
5339   // enough to allow the thread to get to user bytecode execution.
5340   Solaris::min_stack_allowed = MAX2(thr_min_stack(), Solaris::min_stack_allowed);
5341   // If the pagesize of the VM is greater than 8K determine the appropriate
5342   // number of initial guard pages.  The user can change this with the
5343   // command line arguments, if needed.
5344   if (vm_page_size() > 8*K) {
5345     StackYellowPages = 1;
5346     StackRedPages = 1;
5347     StackShadowPages = round_to((StackShadowPages*8*K), vm_page_size()) / vm_page_size();
5348   }
5349 }
5350 
5351 // To install functions for atexit system call
5352 extern "C" {
5353   static void perfMemory_exit_helper() {
5354     perfMemory_exit();
5355   }
5356 }
5357 
5358 // this is called _after_ the global arguments have been parsed
5359 jint os::init_2(void) {
5360   // try to enable extended file IO ASAP, see 6431278
5361   os::Solaris::try_enable_extended_io();
5362 
5363   // Allocate a single page and mark it as readable for safepoint polling.  Also
5364   // use this first mmap call to check support for MAP_ALIGN.
5365   address polling_page = (address)Solaris::mmap_chunk((char*)page_size,
5366                                                       page_size,
5367                                                       MAP_PRIVATE | MAP_ALIGN,
5368                                                       PROT_READ);
5369   if (polling_page == NULL) {
5370     has_map_align = false;
5371     polling_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE,
5372                                                 PROT_READ);
5373   }
5374 
5375   os::set_polling_page(polling_page);
5376 
5377 #ifndef PRODUCT
5378   if( Verbose && PrintMiscellaneous )
5379     tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n", (intptr_t)polling_page);
5380 #endif
5381 
5382   if (!UseMembar) {
5383     address mem_serialize_page = (address)Solaris::mmap_chunk( NULL, page_size, MAP_PRIVATE, PROT_READ | PROT_WRITE );
5384     guarantee( mem_serialize_page != NULL, "mmap Failed for memory serialize page");
5385     os::set_memory_serialize_page( mem_serialize_page );
5386 
5387 #ifndef PRODUCT
5388     if(Verbose && PrintMiscellaneous)
5389       tty->print("[Memory Serialize  Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
5390 #endif
5391 }
5392 
5393   os::large_page_init();
5394 
5395   // Check minimum allowable stack size for thread creation and to initialize
5396   // the java system classes, including StackOverflowError - depends on page
5397   // size.  Add a page for compiler2 recursion in main thread.
5398   // Add in 2*BytesPerWord times page size to account for VM stack during
5399   // class initialization depending on 32 or 64 bit VM.
5400   os::Solaris::min_stack_allowed = MAX2(os::Solaris::min_stack_allowed,
5401             (size_t)(StackYellowPages+StackRedPages+StackShadowPages+
5402                     2*BytesPerWord COMPILER2_PRESENT(+1)) * page_size);
5403 
5404   size_t threadStackSizeInBytes = ThreadStackSize * K;
5405   if (threadStackSizeInBytes != 0 &&
5406     threadStackSizeInBytes < os::Solaris::min_stack_allowed) {
5407     tty->print_cr("\nThe stack size specified is too small, Specify at least %dk",
5408                   os::Solaris::min_stack_allowed/K);
5409     return JNI_ERR;
5410   }
5411 
5412   // For 64kbps there will be a 64kb page size, which makes
5413   // the usable default stack size quite a bit less.  Increase the
5414   // stack for 64kb (or any > than 8kb) pages, this increases
5415   // virtual memory fragmentation (since we're not creating the
5416   // stack on a power of 2 boundary.  The real fix for this
5417   // should be to fix the guard page mechanism.
5418 
5419   if (vm_page_size() > 8*K) {
5420       threadStackSizeInBytes = (threadStackSizeInBytes != 0)
5421          ? threadStackSizeInBytes +
5422            ((StackYellowPages + StackRedPages) * vm_page_size())
5423          : 0;
5424       ThreadStackSize = threadStackSizeInBytes/K;
5425   }
5426 
5427   // Make the stack size a multiple of the page size so that
5428   // the yellow/red zones can be guarded.
5429   JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
5430         vm_page_size()));
5431 
5432   Solaris::libthread_init();
5433 
5434   if (UseNUMA) {
5435     if (!Solaris::liblgrp_init()) {
5436       UseNUMA = false;
5437     } else {
5438       size_t lgrp_limit = os::numa_get_groups_num();
5439       int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit, mtInternal);
5440       size_t lgrp_num = os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
5441       FREE_C_HEAP_ARRAY(int, lgrp_ids, mtInternal);
5442       if (lgrp_num < 2) {
5443         // There's only one locality group, disable NUMA.
5444         UseNUMA = false;
5445       }
5446     }
5447     // ISM is not compatible with the NUMA allocator - it always allocates
5448     // pages round-robin across the lgroups.
5449     if (UseNUMA && UseLargePages && UseISM) {
5450       if (!FLAG_IS_DEFAULT(UseNUMA)) {
5451         if (FLAG_IS_DEFAULT(UseLargePages) && FLAG_IS_DEFAULT(UseISM)) {
5452           UseLargePages = false;
5453         } else {
5454           warning("UseNUMA is not compatible with ISM large pages, disabling NUMA allocator");
5455           UseNUMA = false;
5456         }
5457       } else {
5458         UseNUMA = false;
5459       }
5460     }
5461     if (!UseNUMA && ForceNUMA) {
5462       UseNUMA = true;
5463     }
5464   }
5465 
5466   Solaris::signal_sets_init();
5467   Solaris::init_signal_mem();
5468   Solaris::install_signal_handlers();
5469 
5470   if (libjsigversion < JSIG_VERSION_1_4_1) {
5471     Maxlibjsigsigs = OLDMAXSIGNUM;
5472   }
5473 
5474   // initialize synchronization primitives to use either thread or
5475   // lwp synchronization (controlled by UseLWPSynchronization)
5476   Solaris::synchronization_init();
5477 
5478   if (MaxFDLimit) {
5479     // set the number of file descriptors to max. print out error
5480     // if getrlimit/setrlimit fails but continue regardless.
5481     struct rlimit nbr_files;
5482     int status = getrlimit(RLIMIT_NOFILE, &nbr_files);
5483     if (status != 0) {
5484       if (PrintMiscellaneous && (Verbose || WizardMode))
5485         perror("os::init_2 getrlimit failed");
5486     } else {
5487       nbr_files.rlim_cur = nbr_files.rlim_max;
5488       status = setrlimit(RLIMIT_NOFILE, &nbr_files);
5489       if (status != 0) {
5490         if (PrintMiscellaneous && (Verbose || WizardMode))
5491           perror("os::init_2 setrlimit failed");
5492       }
5493     }
5494   }
5495 
5496   // Calculate theoretical max. size of Threads to guard gainst
5497   // artifical out-of-memory situations, where all available address-
5498   // space has been reserved by thread stacks. Default stack size is 1Mb.
5499   size_t pre_thread_stack_size = (JavaThread::stack_size_at_create()) ?
5500     JavaThread::stack_size_at_create() : (1*K*K);
5501   assert(pre_thread_stack_size != 0, "Must have a stack");
5502   // Solaris has a maximum of 4Gb of user programs. Calculate the thread limit when
5503   // we should start doing Virtual Memory banging. Currently when the threads will
5504   // have used all but 200Mb of space.
5505   size_t max_address_space = ((unsigned int)4 * K * K * K) - (200 * K * K);
5506   Solaris::_os_thread_limit = max_address_space / pre_thread_stack_size;
5507 
5508   // at-exit methods are called in the reverse order of their registration.
5509   // In Solaris 7 and earlier, atexit functions are called on return from
5510   // main or as a result of a call to exit(3C). There can be only 32 of
5511   // these functions registered and atexit() does not set errno. In Solaris
5512   // 8 and later, there is no limit to the number of functions registered
5513   // and atexit() sets errno. In addition, in Solaris 8 and later, atexit
5514   // functions are called upon dlclose(3DL) in addition to return from main
5515   // and exit(3C).
5516 
5517   if (PerfAllowAtExitRegistration) {
5518     // only register atexit functions if PerfAllowAtExitRegistration is set.
5519     // atexit functions can be delayed until process exit time, which
5520     // can be problematic for embedded VM situations. Embedded VMs should
5521     // call DestroyJavaVM() to assure that VM resources are released.
5522 
5523     // note: perfMemory_exit_helper atexit function may be removed in
5524     // the future if the appropriate cleanup code can be added to the
5525     // VM_Exit VMOperation's doit method.
5526     if (atexit(perfMemory_exit_helper) != 0) {
5527       warning("os::init2 atexit(perfMemory_exit_helper) failed");
5528     }
5529   }
5530 
5531   // Init pset_loadavg function pointer
5532   init_pset_getloadavg_ptr();
5533 
5534   return JNI_OK;
5535 }
5536 
5537 void os::init_3(void) {
5538   return;
5539 }
5540 
5541 // Mark the polling page as unreadable
5542 void os::make_polling_page_unreadable(void) {
5543   if( mprotect((char *)_polling_page, page_size, PROT_NONE) != 0 )
5544     fatal("Could not disable polling page");
5545 };
5546 
5547 // Mark the polling page as readable
5548 void os::make_polling_page_readable(void) {
5549   if( mprotect((char *)_polling_page, page_size, PROT_READ) != 0 )
5550     fatal("Could not enable polling page");
5551 };
5552 
5553 // OS interface.
5554 
5555 bool os::check_heap(bool force) { return true; }
5556 
5557 typedef int (*vsnprintf_t)(char* buf, size_t count, const char* fmt, va_list argptr);
5558 static vsnprintf_t sol_vsnprintf = NULL;
5559 
5560 int local_vsnprintf(char* buf, size_t count, const char* fmt, va_list argptr) {
5561   if (!sol_vsnprintf) {
5562     //search  for the named symbol in the objects that were loaded after libjvm
5563     void* where = RTLD_NEXT;
5564     if ((sol_vsnprintf = CAST_TO_FN_PTR(vsnprintf_t, dlsym(where, "__vsnprintf"))) == NULL)
5565         sol_vsnprintf = CAST_TO_FN_PTR(vsnprintf_t, dlsym(where, "vsnprintf"));
5566     if (!sol_vsnprintf){
5567       //search  for the named symbol in the objects that were loaded before libjvm
5568       where = RTLD_DEFAULT;
5569       if ((sol_vsnprintf = CAST_TO_FN_PTR(vsnprintf_t, dlsym(where, "__vsnprintf"))) == NULL)
5570         sol_vsnprintf = CAST_TO_FN_PTR(vsnprintf_t, dlsym(where, "vsnprintf"));
5571       assert(sol_vsnprintf != NULL, "vsnprintf not found");
5572     }
5573   }
5574   return (*sol_vsnprintf)(buf, count, fmt, argptr);
5575 }
5576 
5577 
5578 // Is a (classpath) directory empty?
5579 bool os::dir_is_empty(const char* path) {
5580   DIR *dir = NULL;
5581   struct dirent *ptr;
5582 
5583   dir = opendir(path);
5584   if (dir == NULL) return true;
5585 
5586   /* Scan the directory */
5587   bool result = true;
5588   char buf[sizeof(struct dirent) + MAX_PATH];
5589   struct dirent *dbuf = (struct dirent *) buf;
5590   while (result && (ptr = readdir(dir, dbuf)) != NULL) {
5591     if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
5592       result = false;
5593     }
5594   }
5595   closedir(dir);
5596   return result;
5597 }
5598 
5599 // This code originates from JDK's sysOpen and open64_w
5600 // from src/solaris/hpi/src/system_md.c
5601 
5602 #ifndef O_DELETE
5603 #define O_DELETE 0x10000
5604 #endif
5605 
5606 // Open a file. Unlink the file immediately after open returns
5607 // if the specified oflag has the O_DELETE flag set.
5608 // O_DELETE is used only in j2se/src/share/native/java/util/zip/ZipFile.c
5609 
5610 int os::open(const char *path, int oflag, int mode) {
5611   if (strlen(path) > MAX_PATH - 1) {
5612     errno = ENAMETOOLONG;
5613     return -1;
5614   }
5615   int fd;
5616   int o_delete = (oflag & O_DELETE);
5617   oflag = oflag & ~O_DELETE;
5618 
5619   fd = ::open64(path, oflag, mode);
5620   if (fd == -1) return -1;
5621 
5622   //If the open succeeded, the file might still be a directory
5623   {
5624     struct stat64 buf64;
5625     int ret = ::fstat64(fd, &buf64);
5626     int st_mode = buf64.st_mode;
5627 
5628     if (ret != -1) {
5629       if ((st_mode & S_IFMT) == S_IFDIR) {
5630         errno = EISDIR;
5631         ::close(fd);
5632         return -1;
5633       }
5634     } else {
5635       ::close(fd);
5636       return -1;
5637     }
5638   }
5639     /*
5640      * 32-bit Solaris systems suffer from:
5641      *
5642      * - an historical default soft limit of 256 per-process file
5643      *   descriptors that is too low for many Java programs.
5644      *
5645      * - a design flaw where file descriptors created using stdio
5646      *   fopen must be less than 256, _even_ when the first limit above
5647      *   has been raised.  This can cause calls to fopen (but not calls to
5648      *   open, for example) to fail mysteriously, perhaps in 3rd party
5649      *   native code (although the JDK itself uses fopen).  One can hardly
5650      *   criticize them for using this most standard of all functions.
5651      *
5652      * We attempt to make everything work anyways by:
5653      *
5654      * - raising the soft limit on per-process file descriptors beyond
5655      *   256
5656      *
5657      * - As of Solaris 10u4, we can request that Solaris raise the 256
5658      *   stdio fopen limit by calling function enable_extended_FILE_stdio.
5659      *   This is done in init_2 and recorded in enabled_extended_FILE_stdio
5660      *
5661      * - If we are stuck on an old (pre 10u4) Solaris system, we can
5662      *   workaround the bug by remapping non-stdio file descriptors below
5663      *   256 to ones beyond 256, which is done below.
5664      *
5665      * See:
5666      * 1085341: 32-bit stdio routines should support file descriptors >255
5667      * 6533291: Work around 32-bit Solaris stdio limit of 256 open files
5668      * 6431278: Netbeans crash on 32 bit Solaris: need to call
5669      *          enable_extended_FILE_stdio() in VM initialisation
5670      * Giri Mandalika's blog
5671      * http://technopark02.blogspot.com/2005_05_01_archive.html
5672      */
5673 #ifndef  _LP64
5674      if ((!enabled_extended_FILE_stdio) && fd < 256) {
5675          int newfd = ::fcntl(fd, F_DUPFD, 256);
5676          if (newfd != -1) {
5677              ::close(fd);
5678              fd = newfd;
5679          }
5680      }
5681 #endif // 32-bit Solaris
5682     /*
5683      * All file descriptors that are opened in the JVM and not
5684      * specifically destined for a subprocess should have the
5685      * close-on-exec flag set.  If we don't set it, then careless 3rd
5686      * party native code might fork and exec without closing all
5687      * appropriate file descriptors (e.g. as we do in closeDescriptors in
5688      * UNIXProcess.c), and this in turn might:
5689      *
5690      * - cause end-of-file to fail to be detected on some file
5691      *   descriptors, resulting in mysterious hangs, or
5692      *
5693      * - might cause an fopen in the subprocess to fail on a system
5694      *   suffering from bug 1085341.
5695      *
5696      * (Yes, the default setting of the close-on-exec flag is a Unix
5697      * design flaw)
5698      *
5699      * See:
5700      * 1085341: 32-bit stdio routines should support file descriptors >255
5701      * 4843136: (process) pipe file descriptor from Runtime.exec not being closed
5702      * 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9
5703      */
5704 #ifdef FD_CLOEXEC
5705     {
5706         int flags = ::fcntl(fd, F_GETFD);
5707         if (flags != -1)
5708             ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5709     }
5710 #endif
5711 
5712   if (o_delete != 0) {
5713     ::unlink(path);
5714   }
5715   return fd;
5716 }
5717 
5718 // create binary file, rewriting existing file if required
5719 int os::create_binary_file(const char* path, bool rewrite_existing) {
5720   int oflags = O_WRONLY | O_CREAT;
5721   if (!rewrite_existing) {
5722     oflags |= O_EXCL;
5723   }
5724   return ::open64(path, oflags, S_IREAD | S_IWRITE);
5725 }
5726 
5727 // return current position of file pointer
5728 jlong os::current_file_offset(int fd) {
5729   return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR);
5730 }
5731 
5732 // move file pointer to the specified offset
5733 jlong os::seek_to_file_offset(int fd, jlong offset) {
5734   return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);
5735 }
5736 
5737 jlong os::lseek(int fd, jlong offset, int whence) {
5738   return (jlong) ::lseek64(fd, offset, whence);
5739 }
5740 
5741 char * os::native_path(char *path) {
5742   return path;
5743 }
5744 
5745 int os::ftruncate(int fd, jlong length) {
5746   return ::ftruncate64(fd, length);
5747 }
5748 
5749 int os::fsync(int fd)  {
5750   RESTARTABLE_RETURN_INT(::fsync(fd));
5751 }
5752 
5753 int os::available(int fd, jlong *bytes) {
5754   jlong cur, end;
5755   int mode;
5756   struct stat64 buf64;
5757 
5758   if (::fstat64(fd, &buf64) >= 0) {
5759     mode = buf64.st_mode;
5760     if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
5761       /*
5762       * XXX: is the following call interruptible? If so, this might
5763       * need to go through the INTERRUPT_IO() wrapper as for other
5764       * blocking, interruptible calls in this file.
5765       */
5766       int n,ioctl_return;
5767 
5768       INTERRUPTIBLE(::ioctl(fd, FIONREAD, &n),ioctl_return,os::Solaris::clear_interrupted);
5769       if (ioctl_return>= 0) {
5770           *bytes = n;
5771         return 1;
5772       }
5773     }
5774   }
5775   if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) {
5776     return 0;
5777   } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) {
5778     return 0;
5779   } else if (::lseek64(fd, cur, SEEK_SET) == -1) {
5780     return 0;
5781   }
5782   *bytes = end - cur;
5783   return 1;
5784 }
5785 
5786 // Map a block of memory.
5787 char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
5788                      char *addr, size_t bytes, bool read_only,
5789                      bool allow_exec) {
5790   int prot;
5791   int flags;
5792 
5793   if (read_only) {
5794     prot = PROT_READ;
5795     flags = MAP_SHARED;
5796   } else {
5797     prot = PROT_READ | PROT_WRITE;
5798     flags = MAP_PRIVATE;
5799   }
5800 
5801   if (allow_exec) {
5802     prot |= PROT_EXEC;
5803   }
5804 
5805   if (addr != NULL) {
5806     flags |= MAP_FIXED;
5807   }
5808 
5809   char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
5810                                      fd, file_offset);
5811   if (mapped_address == MAP_FAILED) {
5812     return NULL;
5813   }
5814   return mapped_address;
5815 }
5816 
5817 
5818 // Remap a block of memory.
5819 char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
5820                        char *addr, size_t bytes, bool read_only,
5821                        bool allow_exec) {
5822   // same as map_memory() on this OS
5823   return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
5824                         allow_exec);
5825 }
5826 
5827 
5828 // Unmap a block of memory.
5829 bool os::pd_unmap_memory(char* addr, size_t bytes) {
5830   return munmap(addr, bytes) == 0;
5831 }
5832 
5833 void os::pause() {
5834   char filename[MAX_PATH];
5835   if (PauseAtStartupFile && PauseAtStartupFile[0]) {
5836     jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
5837   } else {
5838     jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
5839   }
5840 
5841   int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
5842   if (fd != -1) {
5843     struct stat buf;
5844     ::close(fd);
5845     while (::stat(filename, &buf) == 0) {
5846       (void)::poll(NULL, 0, 100);
5847     }
5848   } else {
5849     jio_fprintf(stderr,
5850       "Could not open pause file '%s', continuing immediately.\n", filename);
5851   }
5852 }
5853 
5854 #ifndef PRODUCT
5855 #ifdef INTERPOSE_ON_SYSTEM_SYNCH_FUNCTIONS
5856 // Turn this on if you need to trace synch operations.
5857 // Set RECORD_SYNCH_LIMIT to a large-enough value,
5858 // and call record_synch_enable and record_synch_disable
5859 // around the computation of interest.
5860 
5861 void record_synch(char* name, bool returning);  // defined below
5862 
5863 class RecordSynch {
5864   char* _name;
5865  public:
5866   RecordSynch(char* name) :_name(name)
5867                  { record_synch(_name, false); }
5868   ~RecordSynch() { record_synch(_name,   true);  }
5869 };
5870 
5871 #define CHECK_SYNCH_OP(ret, name, params, args, inner)          \
5872 extern "C" ret name params {                                    \
5873   typedef ret name##_t params;                                  \
5874   static name##_t* implem = NULL;                               \
5875   static int callcount = 0;                                     \
5876   if (implem == NULL) {                                         \
5877     implem = (name##_t*) dlsym(RTLD_NEXT, #name);               \
5878     if (implem == NULL)  fatal(dlerror());                      \
5879   }                                                             \
5880   ++callcount;                                                  \
5881   RecordSynch _rs(#name);                                       \
5882   inner;                                                        \
5883   return implem args;                                           \
5884 }
5885 // in dbx, examine callcounts this way:
5886 // for n in $(eval whereis callcount | awk '{print $2}'); do print $n; done
5887 
5888 #define CHECK_POINTER_OK(p) \
5889   (Universe::perm_gen() == NULL || !Universe::is_reserved_heap((oop)(p)))
5890 #define CHECK_MU \
5891   if (!CHECK_POINTER_OK(mu)) fatal("Mutex must be in C heap only.");
5892 #define CHECK_CV \
5893   if (!CHECK_POINTER_OK(cv)) fatal("Condvar must be in C heap only.");
5894 #define CHECK_P(p) \
5895   if (!CHECK_POINTER_OK(p))  fatal(false,  "Pointer must be in C heap only.");
5896 
5897 #define CHECK_MUTEX(mutex_op) \
5898 CHECK_SYNCH_OP(int, mutex_op, (mutex_t *mu), (mu), CHECK_MU);
5899 
5900 CHECK_MUTEX(   mutex_lock)
5901 CHECK_MUTEX(  _mutex_lock)
5902 CHECK_MUTEX( mutex_unlock)
5903 CHECK_MUTEX(_mutex_unlock)
5904 CHECK_MUTEX( mutex_trylock)
5905 CHECK_MUTEX(_mutex_trylock)
5906 
5907 #define CHECK_COND(cond_op) \
5908 CHECK_SYNCH_OP(int, cond_op, (cond_t *cv, mutex_t *mu), (cv, mu), CHECK_MU;CHECK_CV);
5909 
5910 CHECK_COND( cond_wait);
5911 CHECK_COND(_cond_wait);
5912 CHECK_COND(_cond_wait_cancel);
5913 
5914 #define CHECK_COND2(cond_op) \
5915 CHECK_SYNCH_OP(int, cond_op, (cond_t *cv, mutex_t *mu, timestruc_t* ts), (cv, mu, ts), CHECK_MU;CHECK_CV);
5916 
5917 CHECK_COND2( cond_timedwait);
5918 CHECK_COND2(_cond_timedwait);
5919 CHECK_COND2(_cond_timedwait_cancel);
5920 
5921 // do the _lwp_* versions too
5922 #define mutex_t lwp_mutex_t
5923 #define cond_t  lwp_cond_t
5924 CHECK_MUTEX(  _lwp_mutex_lock)
5925 CHECK_MUTEX(  _lwp_mutex_unlock)
5926 CHECK_MUTEX(  _lwp_mutex_trylock)
5927 CHECK_MUTEX( __lwp_mutex_lock)
5928 CHECK_MUTEX( __lwp_mutex_unlock)
5929 CHECK_MUTEX( __lwp_mutex_trylock)
5930 CHECK_MUTEX(___lwp_mutex_lock)
5931 CHECK_MUTEX(___lwp_mutex_unlock)
5932 
5933 CHECK_COND(  _lwp_cond_wait);
5934 CHECK_COND( __lwp_cond_wait);
5935 CHECK_COND(___lwp_cond_wait);
5936 
5937 CHECK_COND2(  _lwp_cond_timedwait);
5938 CHECK_COND2( __lwp_cond_timedwait);
5939 #undef mutex_t
5940 #undef cond_t
5941 
5942 CHECK_SYNCH_OP(int, _lwp_suspend2,       (int lwp, int *n), (lwp, n), 0);
5943 CHECK_SYNCH_OP(int,__lwp_suspend2,       (int lwp, int *n), (lwp, n), 0);
5944 CHECK_SYNCH_OP(int, _lwp_kill,           (int lwp, int n),  (lwp, n), 0);
5945 CHECK_SYNCH_OP(int,__lwp_kill,           (int lwp, int n),  (lwp, n), 0);
5946 CHECK_SYNCH_OP(int, _lwp_sema_wait,      (lwp_sema_t* p),   (p),  CHECK_P(p));
5947 CHECK_SYNCH_OP(int,__lwp_sema_wait,      (lwp_sema_t* p),   (p),  CHECK_P(p));
5948 CHECK_SYNCH_OP(int, _lwp_cond_broadcast, (lwp_cond_t* cv),  (cv), CHECK_CV);
5949 CHECK_SYNCH_OP(int,__lwp_cond_broadcast, (lwp_cond_t* cv),  (cv), CHECK_CV);
5950 
5951 
5952 // recording machinery:
5953 
5954 enum { RECORD_SYNCH_LIMIT = 200 };
5955 char* record_synch_name[RECORD_SYNCH_LIMIT];
5956 void* record_synch_arg0ptr[RECORD_SYNCH_LIMIT];
5957 bool record_synch_returning[RECORD_SYNCH_LIMIT];
5958 thread_t record_synch_thread[RECORD_SYNCH_LIMIT];
5959 int record_synch_count = 0;
5960 bool record_synch_enabled = false;
5961 
5962 // in dbx, examine recorded data this way:
5963 // for n in name arg0ptr returning thread; do print record_synch_$n[0..record_synch_count-1]; done
5964 
5965 void record_synch(char* name, bool returning) {
5966   if (record_synch_enabled) {
5967     if (record_synch_count < RECORD_SYNCH_LIMIT) {
5968       record_synch_name[record_synch_count] = name;
5969       record_synch_returning[record_synch_count] = returning;
5970       record_synch_thread[record_synch_count] = thr_self();
5971       record_synch_arg0ptr[record_synch_count] = &name;
5972       record_synch_count++;
5973     }
5974     // put more checking code here:
5975     // ...
5976   }
5977 }
5978 
5979 void record_synch_enable() {
5980   // start collecting trace data, if not already doing so
5981   if (!record_synch_enabled)  record_synch_count = 0;
5982   record_synch_enabled = true;
5983 }
5984 
5985 void record_synch_disable() {
5986   // stop collecting trace data
5987   record_synch_enabled = false;
5988 }
5989 
5990 #endif // INTERPOSE_ON_SYSTEM_SYNCH_FUNCTIONS
5991 #endif // PRODUCT
5992 
5993 const intptr_t thr_time_off  = (intptr_t)(&((prusage_t *)(NULL))->pr_utime);
5994 const intptr_t thr_time_size = (intptr_t)(&((prusage_t *)(NULL))->pr_ttime) -
5995                                (intptr_t)(&((prusage_t *)(NULL))->pr_utime);
5996 
5997 
5998 // JVMTI & JVM monitoring and management support
5999 // The thread_cpu_time() and current_thread_cpu_time() are only
6000 // supported if is_thread_cpu_time_supported() returns true.
6001 // They are not supported on Solaris T1.
6002 
6003 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
6004 // are used by JVM M&M and JVMTI to get user+sys or user CPU time
6005 // of a thread.
6006 //
6007 // current_thread_cpu_time() and thread_cpu_time(Thread *)
6008 // returns the fast estimate available on the platform.
6009 
6010 // hrtime_t gethrvtime() return value includes
6011 // user time but does not include system time
6012 jlong os::current_thread_cpu_time() {
6013   return (jlong) gethrvtime();
6014 }
6015 
6016 jlong os::thread_cpu_time(Thread *thread) {
6017   // return user level CPU time only to be consistent with
6018   // what current_thread_cpu_time returns.
6019   // thread_cpu_time_info() must be changed if this changes
6020   return os::thread_cpu_time(thread, false /* user time only */);
6021 }
6022 
6023 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
6024   if (user_sys_cpu_time) {
6025     return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
6026   } else {
6027     return os::current_thread_cpu_time();
6028   }
6029 }
6030 
6031 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
6032   char proc_name[64];
6033   int count;
6034   prusage_t prusage;
6035   jlong lwp_time;
6036   int fd;
6037 
6038   sprintf(proc_name, "/proc/%d/lwp/%d/lwpusage",
6039                      getpid(),
6040                      thread->osthread()->lwp_id());
6041   fd = ::open(proc_name, O_RDONLY);
6042   if ( fd == -1 ) return -1;
6043 
6044   do {
6045     count = ::pread(fd,
6046                   (void *)&prusage.pr_utime,
6047                   thr_time_size,
6048                   thr_time_off);
6049   } while (count < 0 && errno == EINTR);
6050   ::close(fd);
6051   if ( count < 0 ) return -1;
6052 
6053   if (user_sys_cpu_time) {
6054     // user + system CPU time
6055     lwp_time = (((jlong)prusage.pr_stime.tv_sec +
6056                  (jlong)prusage.pr_utime.tv_sec) * (jlong)1000000000) +
6057                  (jlong)prusage.pr_stime.tv_nsec +
6058                  (jlong)prusage.pr_utime.tv_nsec;
6059   } else {
6060     // user level CPU time only
6061     lwp_time = ((jlong)prusage.pr_utime.tv_sec * (jlong)1000000000) +
6062                 (jlong)prusage.pr_utime.tv_nsec;
6063   }
6064 
6065   return(lwp_time);
6066 }
6067 
6068 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
6069   info_ptr->max_value = ALL_64_BITS;      // will not wrap in less than 64 bits
6070   info_ptr->may_skip_backward = false;    // elapsed time not wall time
6071   info_ptr->may_skip_forward = false;     // elapsed time not wall time
6072   info_ptr->kind = JVMTI_TIMER_USER_CPU;  // only user time is returned
6073 }
6074 
6075 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
6076   info_ptr->max_value = ALL_64_BITS;      // will not wrap in less than 64 bits
6077   info_ptr->may_skip_backward = false;    // elapsed time not wall time
6078   info_ptr->may_skip_forward = false;     // elapsed time not wall time
6079   info_ptr->kind = JVMTI_TIMER_USER_CPU;  // only user time is returned
6080 }
6081 
6082 bool os::is_thread_cpu_time_supported() {
6083   if ( os::Solaris::T2_libthread() || UseBoundThreads ) {
6084     return true;
6085   } else {
6086     return false;
6087   }
6088 }
6089 
6090 // System loadavg support.  Returns -1 if load average cannot be obtained.
6091 // Return the load average for our processor set if the primitive exists
6092 // (Solaris 9 and later).  Otherwise just return system wide loadavg.
6093 int os::loadavg(double loadavg[], int nelem) {
6094   if (pset_getloadavg_ptr != NULL) {
6095     return (*pset_getloadavg_ptr)(PS_MYID, loadavg, nelem);
6096   } else {
6097     return ::getloadavg(loadavg, nelem);
6098   }
6099 }
6100 
6101 //---------------------------------------------------------------------------------
6102 
6103 static address same_page(address x, address y) {
6104   intptr_t page_bits = -os::vm_page_size();
6105   if ((intptr_t(x) & page_bits) == (intptr_t(y) & page_bits))
6106     return x;
6107   else if (x > y)
6108     return (address)(intptr_t(y) | ~page_bits) + 1;
6109   else
6110     return (address)(intptr_t(y) & page_bits);
6111 }
6112 
6113 bool os::find(address addr, outputStream* st) {
6114   Dl_info dlinfo;
6115   memset(&dlinfo, 0, sizeof(dlinfo));
6116   if (dladdr(addr, &dlinfo)) {
6117 #ifdef _LP64
6118     st->print("0x%016lx: ", addr);
6119 #else
6120     st->print("0x%08x: ", addr);
6121 #endif
6122     if (dlinfo.dli_sname != NULL)
6123       st->print("%s+%#lx", dlinfo.dli_sname, addr-(intptr_t)dlinfo.dli_saddr);
6124     else if (dlinfo.dli_fname)
6125       st->print("<offset %#lx>", addr-(intptr_t)dlinfo.dli_fbase);
6126     else
6127       st->print("<absolute address>");
6128     if (dlinfo.dli_fname)  st->print(" in %s", dlinfo.dli_fname);
6129 #ifdef _LP64
6130     if (dlinfo.dli_fbase)  st->print(" at 0x%016lx", dlinfo.dli_fbase);
6131 #else
6132     if (dlinfo.dli_fbase)  st->print(" at 0x%08x", dlinfo.dli_fbase);
6133 #endif
6134     st->cr();
6135 
6136     if (Verbose) {
6137       // decode some bytes around the PC
6138       address begin = same_page(addr-40, addr);
6139       address end   = same_page(addr+40, addr);
6140       address       lowest = (address) dlinfo.dli_sname;
6141       if (!lowest)  lowest = (address) dlinfo.dli_fbase;
6142       if (begin < lowest)  begin = lowest;
6143       Dl_info dlinfo2;
6144       if (dladdr(end, &dlinfo2) && dlinfo2.dli_saddr != dlinfo.dli_saddr
6145           && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
6146         end = (address) dlinfo2.dli_saddr;
6147       Disassembler::decode(begin, end, st);
6148     }
6149     return true;
6150   }
6151   return false;
6152 }
6153 
6154 // Following function has been added to support HotSparc's libjvm.so running
6155 // under Solaris production JDK 1.2.2 / 1.3.0.  These came from
6156 // src/solaris/hpi/native_threads in the EVM codebase.
6157 //
6158 // NOTE: This is no longer needed in the 1.3.1 and 1.4 production release
6159 // libraries and should thus be removed. We will leave it behind for a while
6160 // until we no longer want to able to run on top of 1.3.0 Solaris production
6161 // JDK. See 4341971.
6162 
6163 #define STACK_SLACK 0x800
6164 
6165 extern "C" {
6166   intptr_t sysThreadAvailableStackWithSlack() {
6167     stack_t st;
6168     intptr_t retval, stack_top;
6169     retval = thr_stksegment(&st);
6170     assert(retval == 0, "incorrect return value from thr_stksegment");
6171     assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
6172     assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
6173     stack_top=(intptr_t)st.ss_sp-st.ss_size;
6174     return ((intptr_t)&stack_top - stack_top - STACK_SLACK);
6175   }
6176 }
6177 
6178 extern "C" {
6179 class ASGCT_CallTrace;
6180 void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext);
6181 }
6182 
6183 
6184 // ObjectMonitor park-unpark infrastructure ...
6185 //
6186 // We implement Solaris and Linux PlatformEvents with the
6187 // obvious condvar-mutex-flag triple.
6188 // Another alternative that works quite well is pipes:
6189 // Each PlatformEvent consists of a pipe-pair.
6190 // The thread associated with the PlatformEvent
6191 // calls park(), which reads from the input end of the pipe.
6192 // Unpark() writes into the other end of the pipe.
6193 // The write-side of the pipe must be set NDELAY.
6194 // Unfortunately pipes consume a large # of handles.
6195 // Native solaris lwp_park() and lwp_unpark() work nicely, too.
6196 // Using pipes for the 1st few threads might be workable, however.
6197 //
6198 // park() is permitted to return spuriously.
6199 // Callers of park() should wrap the call to park() in
6200 // an appropriate loop.  A litmus test for the correct
6201 // usage of park is the following: if park() were modified
6202 // to immediately return 0 your code should still work,
6203 // albeit degenerating to a spin loop.
6204 //
6205 // An interesting optimization for park() is to use a trylock()
6206 // to attempt to acquire the mutex.  If the trylock() fails
6207 // then we know that a concurrent unpark() operation is in-progress.
6208 // in that case the park() code could simply set _count to 0
6209 // and return immediately.  The subsequent park() operation *might*
6210 // return immediately.  That's harmless as the caller of park() is
6211 // expected to loop.  By using trylock() we will have avoided a
6212 // avoided a context switch caused by contention on the per-thread mutex.
6213 //
6214 // TODO-FIXME:
6215 // 1.  Reconcile Doug's JSR166 j.u.c park-unpark with the
6216 //     objectmonitor implementation.
6217 // 2.  Collapse the JSR166 parker event, and the
6218 //     objectmonitor ParkEvent into a single "Event" construct.
6219 // 3.  In park() and unpark() add:
6220 //     assert (Thread::current() == AssociatedWith).
6221 // 4.  add spurious wakeup injection on a -XX:EarlyParkReturn=N switch.
6222 //     1-out-of-N park() operations will return immediately.
6223 //
6224 // _Event transitions in park()
6225 //   -1 => -1 : illegal
6226 //    1 =>  0 : pass - return immediately
6227 //    0 => -1 : block
6228 //
6229 // _Event serves as a restricted-range semaphore.
6230 //
6231 // Another possible encoding of _Event would be with
6232 // explicit "PARKED" == 01b and "SIGNALED" == 10b bits.
6233 //
6234 // TODO-FIXME: add DTRACE probes for:
6235 // 1.   Tx parks
6236 // 2.   Ty unparks Tx
6237 // 3.   Tx resumes from park
6238 
6239 
6240 // value determined through experimentation
6241 #define ROUNDINGFIX 11
6242 
6243 // utility to compute the abstime argument to timedwait.
6244 // TODO-FIXME: switch from compute_abstime() to unpackTime().
6245 
6246 static timestruc_t* compute_abstime(timestruc_t* abstime, jlong millis) {
6247   // millis is the relative timeout time
6248   // abstime will be the absolute timeout time
6249   if (millis < 0)  millis = 0;
6250   struct timeval now;
6251   int status = gettimeofday(&now, NULL);
6252   assert(status == 0, "gettimeofday");
6253   jlong seconds = millis / 1000;
6254   jlong max_wait_period;
6255 
6256   if (UseLWPSynchronization) {
6257     // forward port of fix for 4275818 (not sleeping long enough)
6258     // There was a bug in Solaris 6, 7 and pre-patch 5 of 8 where
6259     // _lwp_cond_timedwait() used a round_down algorithm rather
6260     // than a round_up. For millis less than our roundfactor
6261     // it rounded down to 0 which doesn't meet the spec.
6262     // For millis > roundfactor we may return a bit sooner, but
6263     // since we can not accurately identify the patch level and
6264     // this has already been fixed in Solaris 9 and 8 we will
6265     // leave it alone rather than always rounding down.
6266 
6267     if (millis > 0 && millis < ROUNDINGFIX) millis = ROUNDINGFIX;
6268        // It appears that when we go directly through Solaris _lwp_cond_timedwait()
6269            // the acceptable max time threshold is smaller than for libthread on 2.5.1 and 2.6
6270            max_wait_period = 21000000;
6271   } else {
6272     max_wait_period = 50000000;
6273   }
6274   millis %= 1000;
6275   if (seconds > max_wait_period) {      // see man cond_timedwait(3T)
6276      seconds = max_wait_period;
6277   }
6278   abstime->tv_sec = now.tv_sec  + seconds;
6279   long       usec = now.tv_usec + millis * 1000;
6280   if (usec >= 1000000) {
6281     abstime->tv_sec += 1;
6282     usec -= 1000000;
6283   }
6284   abstime->tv_nsec = usec * 1000;
6285   return abstime;
6286 }
6287 
6288 // Test-and-clear _Event, always leaves _Event set to 0, returns immediately.
6289 // Conceptually TryPark() should be equivalent to park(0).
6290 
6291 int os::PlatformEvent::TryPark() {
6292   for (;;) {
6293     const int v = _Event ;
6294     guarantee ((v == 0) || (v == 1), "invariant") ;
6295     if (Atomic::cmpxchg (0, &_Event, v) == v) return v  ;
6296   }
6297 }
6298 
6299 void os::PlatformEvent::park() {           // AKA: down()
6300   // Invariant: Only the thread associated with the Event/PlatformEvent
6301   // may call park().
6302   int v ;
6303   for (;;) {
6304       v = _Event ;
6305       if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
6306   }
6307   guarantee (v >= 0, "invariant") ;
6308   if (v == 0) {
6309      // Do this the hard way by blocking ...
6310      // See http://monaco.sfbay/detail.jsf?cr=5094058.
6311      // TODO-FIXME: for Solaris SPARC set fprs.FEF=0 prior to parking.
6312      // Only for SPARC >= V8PlusA
6313 #if defined(__sparc) && defined(COMPILER2)
6314      if (ClearFPUAtPark) { _mark_fpu_nosave() ; }
6315 #endif
6316      int status = os::Solaris::mutex_lock(_mutex);
6317      assert_status(status == 0, status,  "mutex_lock");
6318      guarantee (_nParked == 0, "invariant") ;
6319      ++ _nParked ;
6320      while (_Event < 0) {
6321         // for some reason, under 2.7 lwp_cond_wait() may return ETIME ...
6322         // Treat this the same as if the wait was interrupted
6323         // With usr/lib/lwp going to kernel, always handle ETIME
6324         status = os::Solaris::cond_wait(_cond, _mutex);
6325         if (status == ETIME) status = EINTR ;
6326         assert_status(status == 0 || status == EINTR, status, "cond_wait");
6327      }
6328      -- _nParked ;
6329      _Event = 0 ;
6330      status = os::Solaris::mutex_unlock(_mutex);
6331      assert_status(status == 0, status, "mutex_unlock");
6332     // Paranoia to ensure our locked and lock-free paths interact
6333     // correctly with each other.
6334     OrderAccess::fence();
6335   }
6336 }
6337 
6338 int os::PlatformEvent::park(jlong millis) {
6339   guarantee (_nParked == 0, "invariant") ;
6340   int v ;
6341   for (;;) {
6342       v = _Event ;
6343       if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
6344   }
6345   guarantee (v >= 0, "invariant") ;
6346   if (v != 0) return OS_OK ;
6347 
6348   int ret = OS_TIMEOUT;
6349   timestruc_t abst;
6350   compute_abstime (&abst, millis);
6351 
6352   // See http://monaco.sfbay/detail.jsf?cr=5094058.
6353   // For Solaris SPARC set fprs.FEF=0 prior to parking.
6354   // Only for SPARC >= V8PlusA
6355 #if defined(__sparc) && defined(COMPILER2)
6356  if (ClearFPUAtPark) { _mark_fpu_nosave() ; }
6357 #endif
6358   int status = os::Solaris::mutex_lock(_mutex);
6359   assert_status(status == 0, status, "mutex_lock");
6360   guarantee (_nParked == 0, "invariant") ;
6361   ++ _nParked ;
6362   while (_Event < 0) {
6363      int status = os::Solaris::cond_timedwait(_cond, _mutex, &abst);
6364      assert_status(status == 0 || status == EINTR ||
6365                    status == ETIME || status == ETIMEDOUT,
6366                    status, "cond_timedwait");
6367      if (!FilterSpuriousWakeups) break ;                // previous semantics
6368      if (status == ETIME || status == ETIMEDOUT) break ;
6369      // We consume and ignore EINTR and spurious wakeups.
6370   }
6371   -- _nParked ;
6372   if (_Event >= 0) ret = OS_OK ;
6373   _Event = 0 ;
6374   status = os::Solaris::mutex_unlock(_mutex);
6375   assert_status(status == 0, status, "mutex_unlock");
6376   // Paranoia to ensure our locked and lock-free paths interact
6377   // correctly with each other.
6378   OrderAccess::fence();
6379   return ret;
6380 }
6381 
6382 void os::PlatformEvent::unpark() {
6383   // Transitions for _Event:
6384   //    0 :=> 1
6385   //    1 :=> 1
6386   //   -1 :=> either 0 or 1; must signal target thread
6387   //          That is, we can safely transition _Event from -1 to either
6388   //          0 or 1. Forcing 1 is slightly more efficient for back-to-back
6389   //          unpark() calls.
6390   // See also: "Semaphores in Plan 9" by Mullender & Cox
6391   //
6392   // Note: Forcing a transition from "-1" to "1" on an unpark() means
6393   // that it will take two back-to-back park() calls for the owning
6394   // thread to block. This has the benefit of forcing a spurious return
6395   // from the first park() call after an unpark() call which will help
6396   // shake out uses of park() and unpark() without condition variables.
6397 
6398   if (Atomic::xchg(1, &_Event) >= 0) return;
6399 
6400   // If the thread associated with the event was parked, wake it.
6401   // Wait for the thread assoc with the PlatformEvent to vacate.
6402   int status = os::Solaris::mutex_lock(_mutex);
6403   assert_status(status == 0, status, "mutex_lock");
6404   int AnyWaiters = _nParked;
6405   status = os::Solaris::mutex_unlock(_mutex);
6406   assert_status(status == 0, status, "mutex_unlock");
6407   guarantee(AnyWaiters == 0 || AnyWaiters == 1, "invariant");
6408   if (AnyWaiters != 0) {
6409     // We intentional signal *after* dropping the lock
6410     // to avoid a common class of futile wakeups.
6411     status = os::Solaris::cond_signal(_cond);
6412     assert_status(status == 0, status, "cond_signal");
6413   }
6414 }
6415 
6416 // JSR166
6417 // -------------------------------------------------------
6418 
6419 /*
6420  * The solaris and linux implementations of park/unpark are fairly
6421  * conservative for now, but can be improved. They currently use a
6422  * mutex/condvar pair, plus _counter.
6423  * Park decrements _counter if > 0, else does a condvar wait.  Unpark
6424  * sets count to 1 and signals condvar.  Only one thread ever waits
6425  * on the condvar. Contention seen when trying to park implies that someone
6426  * is unparking you, so don't wait. And spurious returns are fine, so there
6427  * is no need to track notifications.
6428  */
6429 
6430 #define MAX_SECS 100000000
6431 /*
6432  * This code is common to linux and solaris and will be moved to a
6433  * common place in dolphin.
6434  *
6435  * The passed in time value is either a relative time in nanoseconds
6436  * or an absolute time in milliseconds. Either way it has to be unpacked
6437  * into suitable seconds and nanoseconds components and stored in the
6438  * given timespec structure.
6439  * Given time is a 64-bit value and the time_t used in the timespec is only
6440  * a signed-32-bit value (except on 64-bit Linux) we have to watch for
6441  * overflow if times way in the future are given. Further on Solaris versions
6442  * prior to 10 there is a restriction (see cond_timedwait) that the specified
6443  * number of seconds, in abstime, is less than current_time  + 100,000,000.
6444  * As it will be 28 years before "now + 100000000" will overflow we can
6445  * ignore overflow and just impose a hard-limit on seconds using the value
6446  * of "now + 100,000,000". This places a limit on the timeout of about 3.17
6447  * years from "now".
6448  */
6449 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {
6450   assert (time > 0, "convertTime");
6451 
6452   struct timeval now;
6453   int status = gettimeofday(&now, NULL);
6454   assert(status == 0, "gettimeofday");
6455 
6456   time_t max_secs = now.tv_sec + MAX_SECS;
6457 
6458   if (isAbsolute) {
6459     jlong secs = time / 1000;
6460     if (secs > max_secs) {
6461       absTime->tv_sec = max_secs;
6462     }
6463     else {
6464       absTime->tv_sec = secs;
6465     }
6466     absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;
6467   }
6468   else {
6469     jlong secs = time / NANOSECS_PER_SEC;
6470     if (secs >= MAX_SECS) {
6471       absTime->tv_sec = max_secs;
6472       absTime->tv_nsec = 0;
6473     }
6474     else {
6475       absTime->tv_sec = now.tv_sec + secs;
6476       absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;
6477       if (absTime->tv_nsec >= NANOSECS_PER_SEC) {
6478         absTime->tv_nsec -= NANOSECS_PER_SEC;
6479         ++absTime->tv_sec; // note: this must be <= max_secs
6480       }
6481     }
6482   }
6483   assert(absTime->tv_sec >= 0, "tv_sec < 0");
6484   assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs");
6485   assert(absTime->tv_nsec >= 0, "tv_nsec < 0");
6486   assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec");
6487 }
6488 
6489 void Parker::park(bool isAbsolute, jlong time) {
6490   // Ideally we'd do something useful while spinning, such
6491   // as calling unpackTime().
6492 
6493   // Optional fast-path check:
6494   // Return immediately if a permit is available.
6495   // We depend on Atomic::xchg() having full barrier semantics
6496   // since we are doing a lock-free update to _counter.
6497   if (Atomic::xchg(0, &_counter) > 0) return;
6498 
6499   // Optional fast-exit: Check interrupt before trying to wait
6500   Thread* thread = Thread::current();
6501   assert(thread->is_Java_thread(), "Must be JavaThread");
6502   JavaThread *jt = (JavaThread *)thread;
6503   if (Thread::is_interrupted(thread, false)) {
6504     return;
6505   }
6506 
6507   // First, demultiplex/decode time arguments
6508   timespec absTime;
6509   if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all
6510     return;
6511   }
6512   if (time > 0) {
6513     // Warning: this code might be exposed to the old Solaris time
6514     // round-down bugs.  Grep "roundingFix" for details.
6515     unpackTime(&absTime, isAbsolute, time);
6516   }
6517 
6518   // Enter safepoint region
6519   // Beware of deadlocks such as 6317397.
6520   // The per-thread Parker:: _mutex is a classic leaf-lock.
6521   // In particular a thread must never block on the Threads_lock while
6522   // holding the Parker:: mutex.  If safepoints are pending both the
6523   // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
6524   ThreadBlockInVM tbivm(jt);
6525 
6526   // Don't wait if cannot get lock since interference arises from
6527   // unblocking.  Also. check interrupt before trying wait
6528   if (Thread::is_interrupted(thread, false) ||
6529       os::Solaris::mutex_trylock(_mutex) != 0) {
6530     return;
6531   }
6532 
6533   int status ;
6534 
6535   if (_counter > 0)  { // no wait needed
6536     _counter = 0;
6537     status = os::Solaris::mutex_unlock(_mutex);
6538     assert (status == 0, "invariant") ;
6539     // Paranoia to ensure our locked and lock-free paths interact
6540     // correctly with each other and Java-level accesses.
6541     OrderAccess::fence();
6542     return;
6543   }
6544 
6545 #ifdef ASSERT
6546   // Don't catch signals while blocked; let the running threads have the signals.
6547   // (This allows a debugger to break into the running thread.)
6548   sigset_t oldsigs;
6549   sigset_t* allowdebug_blocked = os::Solaris::allowdebug_blocked_signals();
6550   thr_sigsetmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);
6551 #endif
6552 
6553   OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
6554   jt->set_suspend_equivalent();
6555   // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
6556 
6557   // Do this the hard way by blocking ...
6558   // See http://monaco.sfbay/detail.jsf?cr=5094058.
6559   // TODO-FIXME: for Solaris SPARC set fprs.FEF=0 prior to parking.
6560   // Only for SPARC >= V8PlusA
6561 #if defined(__sparc) && defined(COMPILER2)
6562   if (ClearFPUAtPark) { _mark_fpu_nosave() ; }
6563 #endif
6564 
6565   if (time == 0) {
6566     status = os::Solaris::cond_wait (_cond, _mutex) ;
6567   } else {
6568     status = os::Solaris::cond_timedwait (_cond, _mutex, &absTime);
6569   }
6570   // Note that an untimed cond_wait() can sometimes return ETIME on older
6571   // versions of the Solaris.
6572   assert_status(status == 0 || status == EINTR ||
6573                 status == ETIME || status == ETIMEDOUT,
6574                 status, "cond_timedwait");
6575 
6576 #ifdef ASSERT
6577   thr_sigsetmask(SIG_SETMASK, &oldsigs, NULL);
6578 #endif
6579   _counter = 0 ;
6580   status = os::Solaris::mutex_unlock(_mutex);
6581   assert_status(status == 0, status, "mutex_unlock") ;
6582   // Paranoia to ensure our locked and lock-free paths interact
6583   // correctly with each other and Java-level accesses.
6584   OrderAccess::fence();
6585 
6586   // If externally suspended while waiting, re-suspend
6587   if (jt->handle_special_suspend_equivalent_condition()) {
6588     jt->java_suspend_self();
6589   }
6590 }
6591 
6592 void Parker::unpark() {
6593   int s, status ;
6594   status = os::Solaris::mutex_lock (_mutex) ;
6595   assert (status == 0, "invariant") ;
6596   s = _counter;
6597   _counter = 1;
6598   status = os::Solaris::mutex_unlock (_mutex) ;
6599   assert (status == 0, "invariant") ;
6600 
6601   if (s < 1) {
6602     status = os::Solaris::cond_signal (_cond) ;
6603     assert (status == 0, "invariant") ;
6604   }
6605 }
6606 
6607 extern char** environ;
6608 
6609 // Run the specified command in a separate process. Return its exit value,
6610 // or -1 on failure (e.g. can't fork a new process).
6611 // Unlike system(), this function can be called from signal handler. It
6612 // doesn't block SIGINT et al.
6613 int os::fork_and_exec(char* cmd) {
6614   char * argv[4];
6615   argv[0] = (char *)"sh";
6616   argv[1] = (char *)"-c";
6617   argv[2] = cmd;
6618   argv[3] = NULL;
6619 
6620   // fork is async-safe, fork1 is not so can't use in signal handler
6621   pid_t pid;
6622   Thread* t = ThreadLocalStorage::get_thread_slow();
6623   if (t != NULL && t->is_inside_signal_handler()) {
6624     pid = fork();
6625   } else {
6626     pid = fork1();
6627   }
6628 
6629   if (pid < 0) {
6630     // fork failed
6631     warning("fork failed: %s", strerror(errno));
6632     return -1;
6633 
6634   } else if (pid == 0) {
6635     // child process
6636 
6637     // try to be consistent with system(), which uses "/usr/bin/sh" on Solaris
6638     execve("/usr/bin/sh", argv, environ);
6639 
6640     // execve failed
6641     _exit(-1);
6642 
6643   } else  {
6644     // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
6645     // care about the actual exit code, for now.
6646 
6647     int status;
6648 
6649     // Wait for the child process to exit.  This returns immediately if
6650     // the child has already exited. */
6651     while (waitpid(pid, &status, 0) < 0) {
6652         switch (errno) {
6653         case ECHILD: return 0;
6654         case EINTR: break;
6655         default: return -1;
6656         }
6657     }
6658 
6659     if (WIFEXITED(status)) {
6660        // The child exited normally; get its exit code.
6661        return WEXITSTATUS(status);
6662     } else if (WIFSIGNALED(status)) {
6663        // The child exited because of a signal
6664        // The best value to return is 0x80 + signal number,
6665        // because that is what all Unix shells do, and because
6666        // it allows callers to distinguish between process exit and
6667        // process death by signal.
6668        return 0x80 + WTERMSIG(status);
6669     } else {
6670        // Unknown exit code; pass it through
6671        return status;
6672     }
6673   }
6674 }
6675 
6676 // is_headless_jre()
6677 //
6678 // Test for the existence of xawt/libmawt.so or libawt_xawt.so
6679 // in order to report if we are running in a headless jre
6680 //
6681 // Since JDK8 xawt/libmawt.so was moved into the same directory
6682 // as libawt.so, and renamed libawt_xawt.so
6683 //
6684 bool os::is_headless_jre() {
6685     struct stat statbuf;
6686     char buf[MAXPATHLEN];
6687     char libmawtpath[MAXPATHLEN];
6688     const char *xawtstr  = "/xawt/libmawt.so";
6689     const char *new_xawtstr = "/libawt_xawt.so";
6690     char *p;
6691 
6692     // Get path to libjvm.so
6693     os::jvm_path(buf, sizeof(buf));
6694 
6695     // Get rid of libjvm.so
6696     p = strrchr(buf, '/');
6697     if (p == NULL) return false;
6698     else *p = '\0';
6699 
6700     // Get rid of client or server
6701     p = strrchr(buf, '/');
6702     if (p == NULL) return false;
6703     else *p = '\0';
6704 
6705     // check xawt/libmawt.so
6706     strcpy(libmawtpath, buf);
6707     strcat(libmawtpath, xawtstr);
6708     if (::stat(libmawtpath, &statbuf) == 0) return false;
6709 
6710     // check libawt_xawt.so
6711     strcpy(libmawtpath, buf);
6712     strcat(libmawtpath, new_xawtstr);
6713     if (::stat(libmawtpath, &statbuf) == 0) return false;
6714 
6715     return true;
6716 }
6717 
6718 size_t os::write(int fd, const void *buf, unsigned int nBytes) {
6719   INTERRUPTIBLE_RETURN_INT(::write(fd, buf, nBytes), os::Solaris::clear_interrupted);
6720 }
6721 
6722 int os::close(int fd) {
6723   RESTARTABLE_RETURN_INT(::close(fd));
6724 }
6725 
6726 int os::socket_close(int fd) {
6727   RESTARTABLE_RETURN_INT(::close(fd));
6728 }
6729 
6730 int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
6731   INTERRUPTIBLE_RETURN_INT((int)::recv(fd, buf, nBytes, flags), os::Solaris::clear_interrupted);
6732 }
6733 
6734 int os::send(int fd, char* buf, size_t nBytes, uint flags) {
6735   INTERRUPTIBLE_RETURN_INT((int)::send(fd, buf, nBytes, flags), os::Solaris::clear_interrupted);
6736 }
6737 
6738 int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
6739   RESTARTABLE_RETURN_INT((int)::send(fd, buf, nBytes, flags));
6740 }
6741 
6742 // As both poll and select can be interrupted by signals, we have to be
6743 // prepared to restart the system call after updating the timeout, unless
6744 // a poll() is done with timeout == -1, in which case we repeat with this
6745 // "wait forever" value.
6746 
6747 int os::timeout(int fd, long timeout) {
6748   int res;
6749   struct timeval t;
6750   julong prevtime, newtime;
6751   static const char* aNull = 0;
6752   struct pollfd pfd;
6753   pfd.fd = fd;
6754   pfd.events = POLLIN;
6755 
6756   gettimeofday(&t, &aNull);
6757   prevtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
6758 
6759   for(;;) {
6760     INTERRUPTIBLE_NORESTART(::poll(&pfd, 1, timeout), res, os::Solaris::clear_interrupted);
6761     if(res == OS_ERR && errno == EINTR) {
6762         if(timeout != -1) {
6763           gettimeofday(&t, &aNull);
6764           newtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec /1000;
6765           timeout -= newtime - prevtime;
6766           if(timeout <= 0)
6767             return OS_OK;
6768           prevtime = newtime;
6769         }
6770     } else return res;
6771   }
6772 }
6773 
6774 int os::connect(int fd, struct sockaddr *him, socklen_t len) {
6775   int _result;
6776   INTERRUPTIBLE_NORESTART(::connect(fd, him, len), _result,\
6777                           os::Solaris::clear_interrupted);
6778 
6779   // Depending on when thread interruption is reset, _result could be
6780   // one of two values when errno == EINTR
6781 
6782   if (((_result == OS_INTRPT) || (_result == OS_ERR))
6783       && (errno == EINTR)) {
6784      /* restarting a connect() changes its errno semantics */
6785      INTERRUPTIBLE(::connect(fd, him, len), _result,\
6786                    os::Solaris::clear_interrupted);
6787      /* undo these changes */
6788      if (_result == OS_ERR) {
6789        if (errno == EALREADY) {
6790          errno = EINPROGRESS; /* fall through */
6791        } else if (errno == EISCONN) {
6792          errno = 0;
6793          return OS_OK;
6794        }
6795      }
6796    }
6797    return _result;
6798  }
6799 
6800 int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
6801   if (fd < 0) {
6802     return OS_ERR;
6803   }
6804   INTERRUPTIBLE_RETURN_INT((int)::accept(fd, him, len),\
6805                            os::Solaris::clear_interrupted);
6806 }
6807 
6808 int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
6809                  sockaddr* from, socklen_t* fromlen) {
6810   INTERRUPTIBLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen),\
6811                            os::Solaris::clear_interrupted);
6812 }
6813 
6814 int os::sendto(int fd, char* buf, size_t len, uint flags,
6815                struct sockaddr* to, socklen_t tolen) {
6816   INTERRUPTIBLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen),\
6817                            os::Solaris::clear_interrupted);
6818 }
6819 
6820 int os::socket_available(int fd, jint *pbytes) {
6821   if (fd < 0) {
6822     return OS_OK;
6823   }
6824   int ret;
6825   RESTARTABLE(::ioctl(fd, FIONREAD, pbytes), ret);
6826   // note: ioctl can return 0 when successful, JVM_SocketAvailable
6827   // is expected to return 0 on failure and 1 on success to the jdk.
6828   return (ret == OS_ERR) ? 0 : 1;
6829 }
6830 
6831 int os::bind(int fd, struct sockaddr* him, socklen_t len) {
6832    INTERRUPTIBLE_RETURN_INT_NORESTART(::bind(fd, him, len),\
6833                                       os::Solaris::clear_interrupted);
6834 }
6835 
6836 // Get the default path to the core file
6837 // Returns the length of the string
6838 int os::get_core_path(char* buffer, size_t bufferSize) {
6839   const char* p = get_current_directory(buffer, bufferSize);
6840 
6841   if (p == NULL) {
6842     assert(p != NULL, "failed to get current directory");
6843     return 0;
6844   }
6845 
6846   return strlen(buffer);
6847 }
6848 
6849 #ifndef PRODUCT
6850 void TestReserveMemorySpecial_test() {
6851   // No tests available for this platform
6852 }
6853 #endif