1 /*
   2  * Copyright (c) 1999, 2018, 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 #include "jvm.h"
  26 #include "logging/log.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "runtime/frame.inline.hpp"
  30 #include "runtime/interfaceSupport.inline.hpp"
  31 #include "runtime/os.hpp"
  32 #include "services/memTracker.hpp"
  33 #include "utilities/align.hpp"
  34 #include "utilities/formatBuffer.hpp"
  35 #include "utilities/macros.hpp"
  36 #include "utilities/vmError.hpp"
  37 
  38 #include <dlfcn.h>
  39 #include <grp.h>
  40 #include <pwd.h>
  41 #include <pthread.h>
  42 #include <signal.h>
  43 #include <sys/mman.h>
  44 #include <sys/resource.h>
  45 #include <sys/utsname.h>
  46 #include <time.h>
  47 #include <unistd.h>
  48 
  49 // Todo: provide a os::get_max_process_id() or similar. Number of processes
  50 // may have been configured, can be read more accurately from proc fs etc.
  51 #ifndef MAX_PID
  52 #define MAX_PID INT_MAX
  53 #endif
  54 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
  55 
  56 #define ROOT_UID 0
  57 
  58 #ifndef MAP_ANONYMOUS
  59   #define MAP_ANONYMOUS MAP_ANON
  60 #endif
  61 
  62 #define check_with_errno(check_type, cond, msg)                             \
  63   do {                                                                      \
  64     int err = errno;                                                        \
  65     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  66                os::errno_name(err));                                        \
  67 } while (false)
  68 
  69 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  70 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  71 
  72 // Check core dump limit and report possible place where core can be found
  73 void os::check_dump_limit(char* buffer, size_t bufferSize) {
  74   if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
  75     jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
  76     VMError::record_coredump_status(buffer, false);
  77     return;
  78   }
  79 
  80   int n;
  81   struct rlimit rlim;
  82   bool success;
  83 
  84   char core_path[PATH_MAX];
  85   n = get_core_path(core_path, PATH_MAX);
  86 
  87   if (n <= 0) {
  88     jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());
  89     success = true;
  90 #ifdef LINUX
  91   } else if (core_path[0] == '"') { // redirect to user process
  92     jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);
  93     success = true;
  94 #endif
  95   } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
  96     jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);
  97     success = true;
  98   } else {
  99     switch(rlim.rlim_cur) {
 100       case RLIM_INFINITY:
 101         jio_snprintf(buffer, bufferSize, "%s", core_path);
 102         success = true;
 103         break;
 104       case 0:
 105         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
 106         success = false;
 107         break;
 108       default:
 109         jio_snprintf(buffer, bufferSize, "%s (max size " UINT64_FORMAT " kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, uint64_t(rlim.rlim_cur) / 1024);
 110         success = true;
 111         break;
 112     }
 113   }
 114 
 115   VMError::record_coredump_status(buffer, success);
 116 }
 117 
 118 int os::get_native_stack(address* stack, int frames, int toSkip) {
 119   int frame_idx = 0;
 120   int num_of_frames;  // number of frames captured
 121   frame fr = os::current_frame();
 122   while (fr.pc() && frame_idx < frames) {
 123     if (toSkip > 0) {
 124       toSkip --;
 125     } else {
 126       stack[frame_idx ++] = fr.pc();
 127     }
 128     if (fr.fp() == NULL || fr.cb() != NULL ||
 129         fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;
 130 
 131     if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {
 132       fr = os::get_sender_for_C_frame(&fr);
 133     } else {
 134       break;
 135     }
 136   }
 137   num_of_frames = frame_idx;
 138   for (; frame_idx < frames; frame_idx ++) {
 139     stack[frame_idx] = NULL;
 140   }
 141 
 142   return num_of_frames;
 143 }
 144 
 145 
 146 bool os::unsetenv(const char* name) {
 147   assert(name != NULL, "Null pointer");
 148   return (::unsetenv(name) == 0);
 149 }
 150 
 151 int os::get_last_error() {
 152   return errno;
 153 }
 154 
 155 bool os::is_debugger_attached() {
 156   // not implemented
 157   return false;
 158 }
 159 
 160 void os::wait_for_keypress_at_exit(void) {
 161   // don't do anything on posix platforms
 162   return;
 163 }
 164 
 165 int os::create_file_for_heap(const char* dir) {
 166 
 167   const char name_template[] = "/jvmheap.XXXXXX";
 168 
 169   char *fullname = (char*)os::malloc((strlen(dir) + strlen(name_template) + 1), mtInternal);
 170   if (fullname == NULL) {
 171     vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));
 172     return -1;
 173   }
 174   (void)strncpy(fullname, dir, strlen(dir)+1);
 175   (void)strncat(fullname, name_template, strlen(name_template));
 176 
 177   os::native_path(fullname);
 178 
 179   sigset_t set, oldset;
 180   int ret = sigfillset(&set);
 181   assert_with_errno(ret == 0, "sigfillset returned error");
 182 
 183   // set the file creation mask.
 184   mode_t file_mode = S_IRUSR | S_IWUSR;
 185 
 186   // create a new file.
 187   int fd = mkstemp(fullname);
 188 
 189   if (fd < 0) {
 190     warning("Could not create file for heap with template %s", fullname);
 191     os::free(fullname);
 192     return -1;
 193   }
 194 
 195   // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.
 196   ret = unlink(fullname);
 197   assert_with_errno(ret == 0, "unlink returned error");
 198 
 199   os::free(fullname);
 200   return fd;
 201 }
 202 
 203 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {
 204   char * addr;
 205   int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;
 206   if (requested_addr != NULL) {
 207     assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");
 208     flags |= MAP_FIXED;
 209   }
 210 
 211   // Map reserved/uncommitted pages PROT_NONE so we fail early if we
 212   // touch an uncommitted page. Otherwise, the read/write might
 213   // succeed if we have enough swap space to back the physical page.
 214   addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
 215                        flags, -1, 0);
 216 
 217   if (addr != MAP_FAILED) {
 218     MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);
 219     return addr;
 220   }
 221   return NULL;
 222 }
 223 
 224 static int util_posix_fallocate(int fd, off_t offset, off_t len) {
 225 #ifdef __APPLE__
 226   fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };
 227   // First we try to get a continuous chunk of disk space
 228   int ret = fcntl(fd, F_PREALLOCATE, &store);
 229   if (ret == -1) {
 230     // Maybe we are too fragmented, try to allocate non-continuous range
 231     store.fst_flags = F_ALLOCATEALL;
 232     ret = fcntl(fd, F_PREALLOCATE, &store);
 233   }
 234   if(ret != -1) {
 235     return ftruncate(fd, len);
 236   }
 237   return -1;
 238 #else
 239   return posix_fallocate(fd, offset, len);
 240 #endif
 241 }
 242 
 243 // Map the given address range to the provided file descriptor.
 244 char* os::map_memory_to_file(char* base, size_t size, int fd) {
 245   assert(fd != -1, "File descriptor is not valid");
 246 
 247   // allocate space for the file
 248   int ret = util_posix_fallocate(fd, 0, (off_t)size);
 249   if (ret != 0) {
 250     vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));
 251     return NULL;
 252   }
 253 
 254   int prot = PROT_READ | PROT_WRITE;
 255   int flags = MAP_SHARED;
 256   if (base != NULL) {
 257     flags |= MAP_FIXED;
 258   }
 259   char* addr = (char*)mmap(base, size, prot, flags, fd, 0);
 260 
 261   if (addr == MAP_FAILED) {
 262     warning("Failed mmap to file. (%s)", os::strerror(errno));
 263     return NULL;
 264   }
 265   if (base != NULL && addr != base) {
 266     if (!os::release_memory(addr, size)) {
 267       warning("Could not release memory on unsuccessful file mapping");
 268     }
 269     return NULL;
 270   }
 271   return addr;
 272 }
 273 
 274 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {
 275   assert(fd != -1, "File descriptor is not valid");
 276   assert(base != NULL, "Base cannot be NULL");
 277 
 278   return map_memory_to_file(base, size, fd);
 279 }
 280 
 281 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
 282 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
 283 // rather than unmapping and remapping the whole chunk to get requested alignment.
 284 char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) {
 285   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
 286       "Alignment must be a multiple of allocation granularity (page size)");
 287   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
 288 
 289   size_t extra_size = size + alignment;
 290   assert(extra_size >= size, "overflow, size is too large to allow alignment");
 291 
 292   char* extra_base;
 293   if (file_desc != -1) {
 294     // For file mapping, we do not call os:reserve_memory(extra_size, NULL, alignment, file_desc) because
 295     // we need to deal with shrinking of the file space later when we release extra memory after alignment.
 296     // We also cannot called os:reserve_memory() with file_desc set to -1 because on aix we might get SHM memory.
 297     // So here to call a helper function while reserve memory for us. After we have a aligned base,
 298     // we will replace anonymous mapping with file mapping.
 299     extra_base = reserve_mmapped_memory(extra_size, NULL);
 300     if (extra_base != NULL) {
 301       MemTracker::record_virtual_memory_reserve((address)extra_base, extra_size, CALLER_PC);
 302     }
 303   } else {
 304     extra_base = os::reserve_memory(extra_size, NULL, alignment);
 305   }
 306 
 307   if (extra_base == NULL) {
 308     return NULL;
 309   }
 310 
 311   // Do manual alignment
 312   char* aligned_base = align_up(extra_base, alignment);
 313 
 314   // [  |                                       |  ]
 315   // ^ extra_base
 316   //    ^ extra_base + begin_offset == aligned_base
 317   //     extra_base + begin_offset + size       ^
 318   //                       extra_base + extra_size ^
 319   // |<>| == begin_offset
 320   //                              end_offset == |<>|
 321   size_t begin_offset = aligned_base - extra_base;
 322   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 323 
 324   if (begin_offset > 0) {
 325       os::release_memory(extra_base, begin_offset);
 326   }
 327 
 328   if (end_offset > 0) {
 329       os::release_memory(extra_base + begin_offset + size, end_offset);
 330   }
 331 
 332   if (file_desc != -1) {
 333     // After we have an aligned address, we can replace anonymous mapping with file mapping
 334     if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) {
 335       vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
 336     }
 337     MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC);
 338   }
 339   return aligned_base;
 340 }
 341 
 342 int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
 343   // All supported POSIX platforms provide C99 semantics.
 344   int result = ::vsnprintf(buf, len, fmt, args);
 345   // If an encoding error occurred (result < 0) then it's not clear
 346   // whether the buffer is NUL terminated, so ensure it is.
 347   if ((result < 0) && (len > 0)) {
 348     buf[len - 1] = '\0';
 349   }
 350   return result;
 351 }
 352 
 353 int os::get_fileno(FILE* fp) {
 354   return NOT_AIX(::)fileno(fp);
 355 }
 356 
 357 struct tm* os::gmtime_pd(const time_t* clock, struct tm*  res) {
 358   return gmtime_r(clock, res);
 359 }
 360 
 361 void os::Posix::print_load_average(outputStream* st) {
 362   st->print("load average:");
 363   double loadavg[3];
 364   os::loadavg(loadavg, 3);
 365   st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
 366   st->cr();
 367 }
 368 
 369 void os::Posix::print_rlimit_info(outputStream* st) {
 370   st->print("rlimit:");
 371   struct rlimit rlim;
 372 
 373   st->print(" STACK ");
 374   getrlimit(RLIMIT_STACK, &rlim);
 375   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 376   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 377 
 378   st->print(", CORE ");
 379   getrlimit(RLIMIT_CORE, &rlim);
 380   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 381   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 382 
 383   // Isn't there on solaris
 384 #if defined(AIX)
 385   st->print(", NPROC ");
 386   st->print("%d", sysconf(_SC_CHILD_MAX));
 387 #elif !defined(SOLARIS)
 388   st->print(", NPROC ");
 389   getrlimit(RLIMIT_NPROC, &rlim);
 390   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 391   else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
 392 #endif
 393 
 394   st->print(", NOFILE ");
 395   getrlimit(RLIMIT_NOFILE, &rlim);
 396   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 397   else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
 398 
 399   st->print(", AS ");
 400   getrlimit(RLIMIT_AS, &rlim);
 401   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 402   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 403 
 404   st->print(", DATA ");
 405   getrlimit(RLIMIT_DATA, &rlim);
 406   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 407   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 408 
 409   st->print(", FSIZE ");
 410   getrlimit(RLIMIT_FSIZE, &rlim);
 411   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 412   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 413 
 414   st->cr();
 415 }
 416 
 417 void os::Posix::print_uname_info(outputStream* st) {
 418   // kernel
 419   st->print("uname:");
 420   struct utsname name;
 421   uname(&name);
 422   st->print("%s ", name.sysname);
 423 #ifdef ASSERT
 424   st->print("%s ", name.nodename);
 425 #endif
 426   st->print("%s ", name.release);
 427   st->print("%s ", name.version);
 428   st->print("%s", name.machine);
 429   st->cr();
 430 }
 431 
 432 void os::Posix::print_umask(outputStream* st, mode_t umsk) {
 433   st->print((umsk & S_IRUSR) ? "r" : "-");
 434   st->print((umsk & S_IWUSR) ? "w" : "-");
 435   st->print((umsk & S_IXUSR) ? "x" : "-");
 436   st->print((umsk & S_IRGRP) ? "r" : "-");
 437   st->print((umsk & S_IWGRP) ? "w" : "-");
 438   st->print((umsk & S_IXGRP) ? "x" : "-");
 439   st->print((umsk & S_IROTH) ? "r" : "-");
 440   st->print((umsk & S_IWOTH) ? "w" : "-");
 441   st->print((umsk & S_IXOTH) ? "x" : "-");
 442 }
 443 
 444 void os::Posix::print_user_info(outputStream* st) {
 445   unsigned id = (unsigned) ::getuid();
 446   st->print("uid  : %u ", id);
 447   id = (unsigned) ::geteuid();
 448   st->print("euid : %u ", id);
 449   id = (unsigned) ::getgid();
 450   st->print("gid  : %u ", id);
 451   id = (unsigned) ::getegid();
 452   st->print_cr("egid : %u", id);
 453   st->cr();
 454 
 455   mode_t umsk = ::umask(0);
 456   ::umask(umsk);
 457   st->print("umask: %04o (", (unsigned) umsk);
 458   print_umask(st, umsk);
 459   st->print_cr(")");
 460   st->cr();
 461 }
 462 
 463 
 464 bool os::get_host_name(char* buf, size_t buflen) {
 465   struct utsname name;
 466   uname(&name);
 467   jio_snprintf(buf, buflen, "%s", name.nodename);
 468   return true;
 469 }
 470 
 471 bool os::has_allocatable_memory_limit(julong* limit) {
 472   struct rlimit rlim;
 473   int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
 474   // if there was an error when calling getrlimit, assume that there is no limitation
 475   // on virtual memory.
 476   bool result;
 477   if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
 478     result = false;
 479   } else {
 480     *limit = (julong)rlim.rlim_cur;
 481     result = true;
 482   }
 483 #ifdef _LP64
 484   return result;
 485 #else
 486   // arbitrary virtual space limit for 32 bit Unices found by testing. If
 487   // getrlimit above returned a limit, bound it with this limit. Otherwise
 488   // directly use it.
 489   const julong max_virtual_limit = (julong)3800*M;
 490   if (result) {
 491     *limit = MIN2(*limit, max_virtual_limit);
 492   } else {
 493     *limit = max_virtual_limit;
 494   }
 495 
 496   // bound by actually allocatable memory. The algorithm uses two bounds, an
 497   // upper and a lower limit. The upper limit is the current highest amount of
 498   // memory that could not be allocated, the lower limit is the current highest
 499   // amount of memory that could be allocated.
 500   // The algorithm iteratively refines the result by halving the difference
 501   // between these limits, updating either the upper limit (if that value could
 502   // not be allocated) or the lower limit (if the that value could be allocated)
 503   // until the difference between these limits is "small".
 504 
 505   // the minimum amount of memory we care about allocating.
 506   const julong min_allocation_size = M;
 507 
 508   julong upper_limit = *limit;
 509 
 510   // first check a few trivial cases
 511   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
 512     *limit = upper_limit;
 513   } else if (!is_allocatable(min_allocation_size)) {
 514     // we found that not even min_allocation_size is allocatable. Return it
 515     // anyway. There is no point to search for a better value any more.
 516     *limit = min_allocation_size;
 517   } else {
 518     // perform the binary search.
 519     julong lower_limit = min_allocation_size;
 520     while ((upper_limit - lower_limit) > min_allocation_size) {
 521       julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
 522       temp_limit = align_down(temp_limit, min_allocation_size);
 523       if (is_allocatable(temp_limit)) {
 524         lower_limit = temp_limit;
 525       } else {
 526         upper_limit = temp_limit;
 527       }
 528     }
 529     *limit = lower_limit;
 530   }
 531   return true;
 532 #endif
 533 }
 534 
 535 const char* os::get_current_directory(char *buf, size_t buflen) {
 536   return getcwd(buf, buflen);
 537 }
 538 
 539 FILE* os::open(int fd, const char* mode) {
 540   return ::fdopen(fd, mode);
 541 }
 542 
 543 void os::flockfile(FILE* fp) {
 544   ::flockfile(fp);
 545 }
 546 
 547 void os::funlockfile(FILE* fp) {
 548   ::funlockfile(fp);
 549 }
 550 
 551 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
 552 // which is used to find statically linked in agents.
 553 // Parameters:
 554 //            sym_name: Symbol in library we are looking for
 555 //            lib_name: Name of library to look in, NULL for shared libs.
 556 //            is_absolute_path == true if lib_name is absolute path to agent
 557 //                                     such as "/a/b/libL.so"
 558 //            == false if only the base name of the library is passed in
 559 //               such as "L"
 560 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
 561                                     bool is_absolute_path) {
 562   char *agent_entry_name;
 563   size_t len;
 564   size_t name_len;
 565   size_t prefix_len = strlen(JNI_LIB_PREFIX);
 566   size_t suffix_len = strlen(JNI_LIB_SUFFIX);
 567   const char *start;
 568 
 569   if (lib_name != NULL) {
 570     name_len = strlen(lib_name);
 571     if (is_absolute_path) {
 572       // Need to strip path, prefix and suffix
 573       if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
 574         lib_name = ++start;
 575       }
 576       if (strlen(lib_name) <= (prefix_len + suffix_len)) {
 577         return NULL;
 578       }
 579       lib_name += prefix_len;
 580       name_len = strlen(lib_name) - suffix_len;
 581     }
 582   }
 583   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 584   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 585   if (agent_entry_name == NULL) {
 586     return NULL;
 587   }
 588   strcpy(agent_entry_name, sym_name);
 589   if (lib_name != NULL) {
 590     strcat(agent_entry_name, "_");
 591     strncat(agent_entry_name, lib_name, name_len);
 592   }
 593   return agent_entry_name;
 594 }
 595 
 596 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
 597   assert(thread == Thread::current(),  "thread consistency check");
 598 
 599   ParkEvent * const slp = thread->_SleepEvent ;
 600   slp->reset() ;
 601   OrderAccess::fence() ;
 602 
 603   if (interruptible) {
 604     jlong prevtime = javaTimeNanos();
 605 
 606     for (;;) {
 607       if (os::is_interrupted(thread, true)) {
 608         return OS_INTRPT;
 609       }
 610 
 611       jlong newtime = javaTimeNanos();
 612 
 613       if (newtime - prevtime < 0) {
 614         // time moving backwards, should only happen if no monotonic clock
 615         // not a guarantee() because JVM should not abort on kernel/glibc bugs
 616         assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected in os::sleep(interruptible)");
 617       } else {
 618         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
 619       }
 620 
 621       if (millis <= 0) {
 622         return OS_OK;
 623       }
 624 
 625       prevtime = newtime;
 626 
 627       {
 628         assert(thread->is_Java_thread(), "sanity check");
 629         JavaThread *jt = (JavaThread *) thread;
 630         ThreadBlockInVM tbivm(jt);
 631         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
 632 
 633         jt->set_suspend_equivalent();
 634         // cleared by handle_special_suspend_equivalent_condition() or
 635         // java_suspend_self() via check_and_wait_while_suspended()
 636 
 637         slp->park(millis);
 638 
 639         // were we externally suspended while we were waiting?
 640         jt->check_and_wait_while_suspended();
 641       }
 642     }
 643   } else {
 644     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
 645     jlong prevtime = javaTimeNanos();
 646 
 647     for (;;) {
 648       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
 649       // the 1st iteration ...
 650       jlong newtime = javaTimeNanos();
 651 
 652       if (newtime - prevtime < 0) {
 653         // time moving backwards, should only happen if no monotonic clock
 654         // not a guarantee() because JVM should not abort on kernel/glibc bugs
 655         assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected on os::sleep(!interruptible)");
 656       } else {
 657         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
 658       }
 659 
 660       if (millis <= 0) break ;
 661 
 662       prevtime = newtime;
 663       slp->park(millis);
 664     }
 665     return OS_OK ;
 666   }
 667 }
 668 
 669 ////////////////////////////////////////////////////////////////////////////////
 670 // interrupt support
 671 
 672 void os::interrupt(Thread* thread) {
 673   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 674 
 675   OSThread* osthread = thread->osthread();
 676 
 677   if (!osthread->interrupted()) {
 678     osthread->set_interrupted(true);
 679     // More than one thread can get here with the same value of osthread,
 680     // resulting in multiple notifications.  We do, however, want the store
 681     // to interrupted() to be visible to other threads before we execute unpark().
 682     OrderAccess::fence();
 683     ParkEvent * const slp = thread->_SleepEvent ;
 684     if (slp != NULL) slp->unpark() ;
 685   }
 686 
 687   // For JSR166. Unpark even if interrupt status already was set
 688   if (thread->is_Java_thread())
 689     ((JavaThread*)thread)->parker()->unpark();
 690 
 691   ParkEvent * ev = thread->_ParkEvent ;
 692   if (ev != NULL) ev->unpark() ;
 693 }
 694 
 695 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
 696   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 697 
 698   OSThread* osthread = thread->osthread();
 699 
 700   bool interrupted = osthread->interrupted();
 701 
 702   // NOTE that since there is no "lock" around the interrupt and
 703   // is_interrupted operations, there is the possibility that the
 704   // interrupted flag (in osThread) will be "false" but that the
 705   // low-level events will be in the signaled state. This is
 706   // intentional. The effect of this is that Object.wait() and
 707   // LockSupport.park() will appear to have a spurious wakeup, which
 708   // is allowed and not harmful, and the possibility is so rare that
 709   // it is not worth the added complexity to add yet another lock.
 710   // For the sleep event an explicit reset is performed on entry
 711   // to os::sleep, so there is no early return. It has also been
 712   // recommended not to put the interrupted flag into the "event"
 713   // structure because it hides the issue.
 714   if (interrupted && clear_interrupted) {
 715     osthread->set_interrupted(false);
 716     // consider thread->_SleepEvent->reset() ... optional optimization
 717   }
 718 
 719   return interrupted;
 720 }
 721 
 722 
 723 
 724 static const struct {
 725   int sig; const char* name;
 726 }
 727  g_signal_info[] =
 728   {
 729   {  SIGABRT,     "SIGABRT" },
 730 #ifdef SIGAIO
 731   {  SIGAIO,      "SIGAIO" },
 732 #endif
 733   {  SIGALRM,     "SIGALRM" },
 734 #ifdef SIGALRM1
 735   {  SIGALRM1,    "SIGALRM1" },
 736 #endif
 737   {  SIGBUS,      "SIGBUS" },
 738 #ifdef SIGCANCEL
 739   {  SIGCANCEL,   "SIGCANCEL" },
 740 #endif
 741   {  SIGCHLD,     "SIGCHLD" },
 742 #ifdef SIGCLD
 743   {  SIGCLD,      "SIGCLD" },
 744 #endif
 745   {  SIGCONT,     "SIGCONT" },
 746 #ifdef SIGCPUFAIL
 747   {  SIGCPUFAIL,  "SIGCPUFAIL" },
 748 #endif
 749 #ifdef SIGDANGER
 750   {  SIGDANGER,   "SIGDANGER" },
 751 #endif
 752 #ifdef SIGDIL
 753   {  SIGDIL,      "SIGDIL" },
 754 #endif
 755 #ifdef SIGEMT
 756   {  SIGEMT,      "SIGEMT" },
 757 #endif
 758   {  SIGFPE,      "SIGFPE" },
 759 #ifdef SIGFREEZE
 760   {  SIGFREEZE,   "SIGFREEZE" },
 761 #endif
 762 #ifdef SIGGFAULT
 763   {  SIGGFAULT,   "SIGGFAULT" },
 764 #endif
 765 #ifdef SIGGRANT
 766   {  SIGGRANT,    "SIGGRANT" },
 767 #endif
 768   {  SIGHUP,      "SIGHUP" },
 769   {  SIGILL,      "SIGILL" },
 770   {  SIGINT,      "SIGINT" },
 771 #ifdef SIGIO
 772   {  SIGIO,       "SIGIO" },
 773 #endif
 774 #ifdef SIGIOINT
 775   {  SIGIOINT,    "SIGIOINT" },
 776 #endif
 777 #ifdef SIGIOT
 778 // SIGIOT is there for BSD compatibility, but on most Unices just a
 779 // synonym for SIGABRT. The result should be "SIGABRT", not
 780 // "SIGIOT".
 781 #if (SIGIOT != SIGABRT )
 782   {  SIGIOT,      "SIGIOT" },
 783 #endif
 784 #endif
 785 #ifdef SIGKAP
 786   {  SIGKAP,      "SIGKAP" },
 787 #endif
 788   {  SIGKILL,     "SIGKILL" },
 789 #ifdef SIGLOST
 790   {  SIGLOST,     "SIGLOST" },
 791 #endif
 792 #ifdef SIGLWP
 793   {  SIGLWP,      "SIGLWP" },
 794 #endif
 795 #ifdef SIGLWPTIMER
 796   {  SIGLWPTIMER, "SIGLWPTIMER" },
 797 #endif
 798 #ifdef SIGMIGRATE
 799   {  SIGMIGRATE,  "SIGMIGRATE" },
 800 #endif
 801 #ifdef SIGMSG
 802   {  SIGMSG,      "SIGMSG" },
 803 #endif
 804   {  SIGPIPE,     "SIGPIPE" },
 805 #ifdef SIGPOLL
 806   {  SIGPOLL,     "SIGPOLL" },
 807 #endif
 808 #ifdef SIGPRE
 809   {  SIGPRE,      "SIGPRE" },
 810 #endif
 811   {  SIGPROF,     "SIGPROF" },
 812 #ifdef SIGPTY
 813   {  SIGPTY,      "SIGPTY" },
 814 #endif
 815 #ifdef SIGPWR
 816   {  SIGPWR,      "SIGPWR" },
 817 #endif
 818   {  SIGQUIT,     "SIGQUIT" },
 819 #ifdef SIGRECONFIG
 820   {  SIGRECONFIG, "SIGRECONFIG" },
 821 #endif
 822 #ifdef SIGRECOVERY
 823   {  SIGRECOVERY, "SIGRECOVERY" },
 824 #endif
 825 #ifdef SIGRESERVE
 826   {  SIGRESERVE,  "SIGRESERVE" },
 827 #endif
 828 #ifdef SIGRETRACT
 829   {  SIGRETRACT,  "SIGRETRACT" },
 830 #endif
 831 #ifdef SIGSAK
 832   {  SIGSAK,      "SIGSAK" },
 833 #endif
 834   {  SIGSEGV,     "SIGSEGV" },
 835 #ifdef SIGSOUND
 836   {  SIGSOUND,    "SIGSOUND" },
 837 #endif
 838 #ifdef SIGSTKFLT
 839   {  SIGSTKFLT,    "SIGSTKFLT" },
 840 #endif
 841   {  SIGSTOP,     "SIGSTOP" },
 842   {  SIGSYS,      "SIGSYS" },
 843 #ifdef SIGSYSERROR
 844   {  SIGSYSERROR, "SIGSYSERROR" },
 845 #endif
 846 #ifdef SIGTALRM
 847   {  SIGTALRM,    "SIGTALRM" },
 848 #endif
 849   {  SIGTERM,     "SIGTERM" },
 850 #ifdef SIGTHAW
 851   {  SIGTHAW,     "SIGTHAW" },
 852 #endif
 853   {  SIGTRAP,     "SIGTRAP" },
 854 #ifdef SIGTSTP
 855   {  SIGTSTP,     "SIGTSTP" },
 856 #endif
 857   {  SIGTTIN,     "SIGTTIN" },
 858   {  SIGTTOU,     "SIGTTOU" },
 859 #ifdef SIGURG
 860   {  SIGURG,      "SIGURG" },
 861 #endif
 862   {  SIGUSR1,     "SIGUSR1" },
 863   {  SIGUSR2,     "SIGUSR2" },
 864 #ifdef SIGVIRT
 865   {  SIGVIRT,     "SIGVIRT" },
 866 #endif
 867   {  SIGVTALRM,   "SIGVTALRM" },
 868 #ifdef SIGWAITING
 869   {  SIGWAITING,  "SIGWAITING" },
 870 #endif
 871 #ifdef SIGWINCH
 872   {  SIGWINCH,    "SIGWINCH" },
 873 #endif
 874 #ifdef SIGWINDOW
 875   {  SIGWINDOW,   "SIGWINDOW" },
 876 #endif
 877   {  SIGXCPU,     "SIGXCPU" },
 878   {  SIGXFSZ,     "SIGXFSZ" },
 879 #ifdef SIGXRES
 880   {  SIGXRES,     "SIGXRES" },
 881 #endif
 882   { -1, NULL }
 883 };
 884 
 885 // Returned string is a constant. For unknown signals "UNKNOWN" is returned.
 886 const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) {
 887 
 888   const char* ret = NULL;
 889 
 890 #ifdef SIGRTMIN
 891   if (sig >= SIGRTMIN && sig <= SIGRTMAX) {
 892     if (sig == SIGRTMIN) {
 893       ret = "SIGRTMIN";
 894     } else if (sig == SIGRTMAX) {
 895       ret = "SIGRTMAX";
 896     } else {
 897       jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN);
 898       return out;
 899     }
 900   }
 901 #endif
 902 
 903   if (sig > 0) {
 904     for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) {
 905       if (g_signal_info[idx].sig == sig) {
 906         ret = g_signal_info[idx].name;
 907         break;
 908       }
 909     }
 910   }
 911 
 912   if (!ret) {
 913     if (!is_valid_signal(sig)) {
 914       ret = "INVALID";
 915     } else {
 916       ret = "UNKNOWN";
 917     }
 918   }
 919 
 920   if (out && outlen > 0) {
 921     strncpy(out, ret, outlen);
 922     out[outlen - 1] = '\0';
 923   }
 924   return out;
 925 }
 926 
 927 int os::Posix::get_signal_number(const char* signal_name) {
 928   char tmp[30];
 929   const char* s = signal_name;
 930   if (s[0] != 'S' || s[1] != 'I' || s[2] != 'G') {
 931     jio_snprintf(tmp, sizeof(tmp), "SIG%s", signal_name);
 932     s = tmp;
 933   }
 934   for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) {
 935     if (strcmp(g_signal_info[idx].name, s) == 0) {
 936       return g_signal_info[idx].sig;
 937     }
 938   }
 939   return -1;
 940 }
 941 
 942 int os::get_signal_number(const char* signal_name) {
 943   return os::Posix::get_signal_number(signal_name);
 944 }
 945 
 946 // Returns true if signal number is valid.
 947 bool os::Posix::is_valid_signal(int sig) {
 948   // MacOS not really POSIX compliant: sigaddset does not return
 949   // an error for invalid signal numbers. However, MacOS does not
 950   // support real time signals and simply seems to have just 33
 951   // signals with no holes in the signal range.
 952 #ifdef __APPLE__
 953   return sig >= 1 && sig < NSIG;
 954 #else
 955   // Use sigaddset to check for signal validity.
 956   sigset_t set;
 957   sigemptyset(&set);
 958   if (sigaddset(&set, sig) == -1 && errno == EINVAL) {
 959     return false;
 960   }
 961   return true;
 962 #endif
 963 }
 964 
 965 bool os::Posix::is_sig_ignored(int sig) {
 966   struct sigaction oact;
 967   sigaction(sig, (struct sigaction*)NULL, &oact);
 968   void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oact.sa_sigaction)
 969                                  : CAST_FROM_FN_PTR(void*,  oact.sa_handler);
 970   if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) {
 971     return true;
 972   } else {
 973     return false;
 974   }
 975 }
 976 
 977 // Returns:
 978 // NULL for an invalid signal number
 979 // "SIG<num>" for a valid but unknown signal number
 980 // signal name otherwise.
 981 const char* os::exception_name(int sig, char* buf, size_t size) {
 982   if (!os::Posix::is_valid_signal(sig)) {
 983     return NULL;
 984   }
 985   const char* const name = os::Posix::get_signal_name(sig, buf, size);
 986   if (strcmp(name, "UNKNOWN") == 0) {
 987     jio_snprintf(buf, size, "SIG%d", sig);
 988   }
 989   return buf;
 990 }
 991 
 992 #define NUM_IMPORTANT_SIGS 32
 993 // Returns one-line short description of a signal set in a user provided buffer.
 994 const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) {
 995   assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size");
 996   // Note: for shortness, just print out the first 32. That should
 997   // cover most of the useful ones, apart from realtime signals.
 998   for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) {
 999     const int rc = sigismember(set, sig);
1000     if (rc == -1 && errno == EINVAL) {
1001       buffer[sig-1] = '?';
1002     } else {
1003       buffer[sig-1] = rc == 0 ? '0' : '1';
1004     }
1005   }
1006   buffer[NUM_IMPORTANT_SIGS] = 0;
1007   return buffer;
1008 }
1009 
1010 // Prints one-line description of a signal set.
1011 void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) {
1012   char buf[NUM_IMPORTANT_SIGS + 1];
1013   os::Posix::describe_signal_set_short(set, buf, sizeof(buf));
1014   st->print("%s", buf);
1015 }
1016 
1017 // Writes one-line description of a combination of sigaction.sa_flags into a user
1018 // provided buffer. Returns that buffer.
1019 const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) {
1020   char* p = buffer;
1021   size_t remaining = size;
1022   bool first = true;
1023   int idx = 0;
1024 
1025   assert(buffer, "invalid argument");
1026 
1027   if (size == 0) {
1028     return buffer;
1029   }
1030 
1031   strncpy(buffer, "none", size);
1032 
1033   const struct {
1034     // NB: i is an unsigned int here because SA_RESETHAND is on some
1035     // systems 0x80000000, which is implicitly unsigned.  Assignining
1036     // it to an int field would be an overflow in unsigned-to-signed
1037     // conversion.
1038     unsigned int i;
1039     const char* s;
1040   } flaginfo [] = {
1041     { SA_NOCLDSTOP, "SA_NOCLDSTOP" },
1042     { SA_ONSTACK,   "SA_ONSTACK"   },
1043     { SA_RESETHAND, "SA_RESETHAND" },
1044     { SA_RESTART,   "SA_RESTART"   },
1045     { SA_SIGINFO,   "SA_SIGINFO"   },
1046     { SA_NOCLDWAIT, "SA_NOCLDWAIT" },
1047     { SA_NODEFER,   "SA_NODEFER"   },
1048 #ifdef AIX
1049     { SA_ONSTACK,   "SA_ONSTACK"   },
1050     { SA_OLDSTYLE,  "SA_OLDSTYLE"  },
1051 #endif
1052     { 0, NULL }
1053   };
1054 
1055   for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) {
1056     if (flags & flaginfo[idx].i) {
1057       if (first) {
1058         jio_snprintf(p, remaining, "%s", flaginfo[idx].s);
1059         first = false;
1060       } else {
1061         jio_snprintf(p, remaining, "|%s", flaginfo[idx].s);
1062       }
1063       const size_t len = strlen(p);
1064       p += len;
1065       remaining -= len;
1066     }
1067   }
1068 
1069   buffer[size - 1] = '\0';
1070 
1071   return buffer;
1072 }
1073 
1074 // Prints one-line description of a combination of sigaction.sa_flags.
1075 void os::Posix::print_sa_flags(outputStream* st, int flags) {
1076   char buffer[0x100];
1077   os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer));
1078   st->print("%s", buffer);
1079 }
1080 
1081 // Helper function for os::Posix::print_siginfo_...():
1082 // return a textual description for signal code.
1083 struct enum_sigcode_desc_t {
1084   const char* s_name;
1085   const char* s_desc;
1086 };
1087 
1088 static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) {
1089 
1090   const struct {
1091     int sig; int code; const char* s_code; const char* s_desc;
1092   } t1 [] = {
1093     { SIGILL,  ILL_ILLOPC,   "ILL_ILLOPC",   "Illegal opcode." },
1094     { SIGILL,  ILL_ILLOPN,   "ILL_ILLOPN",   "Illegal operand." },
1095     { SIGILL,  ILL_ILLADR,   "ILL_ILLADR",   "Illegal addressing mode." },
1096     { SIGILL,  ILL_ILLTRP,   "ILL_ILLTRP",   "Illegal trap." },
1097     { SIGILL,  ILL_PRVOPC,   "ILL_PRVOPC",   "Privileged opcode." },
1098     { SIGILL,  ILL_PRVREG,   "ILL_PRVREG",   "Privileged register." },
1099     { SIGILL,  ILL_COPROC,   "ILL_COPROC",   "Coprocessor error." },
1100     { SIGILL,  ILL_BADSTK,   "ILL_BADSTK",   "Internal stack error." },
1101 #if defined(IA64) && defined(LINUX)
1102     { SIGILL,  ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" },
1103     { SIGILL,  ILL_BREAK,    "ILL_BREAK",    "Application Break instruction" },
1104 #endif
1105     { SIGFPE,  FPE_INTDIV,   "FPE_INTDIV",   "Integer divide by zero." },
1106     { SIGFPE,  FPE_INTOVF,   "FPE_INTOVF",   "Integer overflow." },
1107     { SIGFPE,  FPE_FLTDIV,   "FPE_FLTDIV",   "Floating-point divide by zero." },
1108     { SIGFPE,  FPE_FLTOVF,   "FPE_FLTOVF",   "Floating-point overflow." },
1109     { SIGFPE,  FPE_FLTUND,   "FPE_FLTUND",   "Floating-point underflow." },
1110     { SIGFPE,  FPE_FLTRES,   "FPE_FLTRES",   "Floating-point inexact result." },
1111     { SIGFPE,  FPE_FLTINV,   "FPE_FLTINV",   "Invalid floating-point operation." },
1112     { SIGFPE,  FPE_FLTSUB,   "FPE_FLTSUB",   "Subscript out of range." },
1113     { SIGSEGV, SEGV_MAPERR,  "SEGV_MAPERR",  "Address not mapped to object." },
1114     { SIGSEGV, SEGV_ACCERR,  "SEGV_ACCERR",  "Invalid permissions for mapped object." },
1115 #ifdef AIX
1116     // no explanation found what keyerr would be
1117     { SIGSEGV, SEGV_KEYERR,  "SEGV_KEYERR",  "key error" },
1118 #endif
1119 #if defined(IA64) && !defined(AIX)
1120     { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" },
1121 #endif
1122 #if defined(__sparc) && defined(SOLARIS)
1123 // define Solaris Sparc M7 ADI SEGV signals
1124 #if !defined(SEGV_ACCADI)
1125 #define SEGV_ACCADI 3
1126 #endif
1127     { SIGSEGV, SEGV_ACCADI,  "SEGV_ACCADI",  "ADI not enabled for mapped object." },
1128 #if !defined(SEGV_ACCDERR)
1129 #define SEGV_ACCDERR 4
1130 #endif
1131     { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." },
1132 #if !defined(SEGV_ACCPERR)
1133 #define SEGV_ACCPERR 5
1134 #endif
1135     { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." },
1136 #endif // defined(__sparc) && defined(SOLARIS)
1137     { SIGBUS,  BUS_ADRALN,   "BUS_ADRALN",   "Invalid address alignment." },
1138     { SIGBUS,  BUS_ADRERR,   "BUS_ADRERR",   "Nonexistent physical address." },
1139     { SIGBUS,  BUS_OBJERR,   "BUS_OBJERR",   "Object-specific hardware error." },
1140     { SIGTRAP, TRAP_BRKPT,   "TRAP_BRKPT",   "Process breakpoint." },
1141     { SIGTRAP, TRAP_TRACE,   "TRAP_TRACE",   "Process trace trap." },
1142     { SIGCHLD, CLD_EXITED,   "CLD_EXITED",   "Child has exited." },
1143     { SIGCHLD, CLD_KILLED,   "CLD_KILLED",   "Child has terminated abnormally and did not create a core file." },
1144     { SIGCHLD, CLD_DUMPED,   "CLD_DUMPED",   "Child has terminated abnormally and created a core file." },
1145     { SIGCHLD, CLD_TRAPPED,  "CLD_TRAPPED",  "Traced child has trapped." },
1146     { SIGCHLD, CLD_STOPPED,  "CLD_STOPPED",  "Child has stopped." },
1147     { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." },
1148 #ifdef SIGPOLL
1149     { SIGPOLL, POLL_OUT,     "POLL_OUT",     "Output buffers available." },
1150     { SIGPOLL, POLL_MSG,     "POLL_MSG",     "Input message available." },
1151     { SIGPOLL, POLL_ERR,     "POLL_ERR",     "I/O error." },
1152     { SIGPOLL, POLL_PRI,     "POLL_PRI",     "High priority input available." },
1153     { SIGPOLL, POLL_HUP,     "POLL_HUP",     "Device disconnected. [Option End]" },
1154 #endif
1155     { -1, -1, NULL, NULL }
1156   };
1157 
1158   // Codes valid in any signal context.
1159   const struct {
1160     int code; const char* s_code; const char* s_desc;
1161   } t2 [] = {
1162     { SI_USER,      "SI_USER",     "Signal sent by kill()." },
1163     { SI_QUEUE,     "SI_QUEUE",    "Signal sent by the sigqueue()." },
1164     { SI_TIMER,     "SI_TIMER",    "Signal generated by expiration of a timer set by timer_settime()." },
1165     { SI_ASYNCIO,   "SI_ASYNCIO",  "Signal generated by completion of an asynchronous I/O request." },
1166     { SI_MESGQ,     "SI_MESGQ",    "Signal generated by arrival of a message on an empty message queue." },
1167     // Linux specific
1168 #ifdef SI_TKILL
1169     { SI_TKILL,     "SI_TKILL",    "Signal sent by tkill (pthread_kill)" },
1170 #endif
1171 #ifdef SI_DETHREAD
1172     { SI_DETHREAD,  "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" },
1173 #endif
1174 #ifdef SI_KERNEL
1175     { SI_KERNEL,    "SI_KERNEL",   "Signal sent by kernel." },
1176 #endif
1177 #ifdef SI_SIGIO
1178     { SI_SIGIO,     "SI_SIGIO",    "Signal sent by queued SIGIO" },
1179 #endif
1180 
1181 #ifdef AIX
1182     { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" },
1183     { SI_EMPTY,     "SI_EMPTY",    "siginfo contains no useful information" },
1184 #endif
1185 
1186 #ifdef __sun
1187     { SI_NOINFO,    "SI_NOINFO",   "No signal information" },
1188     { SI_RCTL,      "SI_RCTL",     "kernel generated signal via rctl action" },
1189     { SI_LWP,       "SI_LWP",      "Signal sent via lwp_kill" },
1190 #endif
1191 
1192     { -1, NULL, NULL }
1193   };
1194 
1195   const char* s_code = NULL;
1196   const char* s_desc = NULL;
1197 
1198   for (int i = 0; t1[i].sig != -1; i ++) {
1199     if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) {
1200       s_code = t1[i].s_code;
1201       s_desc = t1[i].s_desc;
1202       break;
1203     }
1204   }
1205 
1206   if (s_code == NULL) {
1207     for (int i = 0; t2[i].s_code != NULL; i ++) {
1208       if (t2[i].code == si->si_code) {
1209         s_code = t2[i].s_code;
1210         s_desc = t2[i].s_desc;
1211       }
1212     }
1213   }
1214 
1215   if (s_code == NULL) {
1216     out->s_name = "unknown";
1217     out->s_desc = "unknown";
1218     return false;
1219   }
1220 
1221   out->s_name = s_code;
1222   out->s_desc = s_desc;
1223 
1224   return true;
1225 }
1226 
1227 void os::print_siginfo(outputStream* os, const void* si0) {
1228 
1229   const siginfo_t* const si = (const siginfo_t*) si0;
1230 
1231   char buf[20];
1232   os->print("siginfo:");
1233 
1234   if (!si) {
1235     os->print(" <null>");
1236     return;
1237   }
1238 
1239   const int sig = si->si_signo;
1240 
1241   os->print(" si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf)));
1242 
1243   enum_sigcode_desc_t ed;
1244   get_signal_code_description(si, &ed);
1245   os->print(", si_code: %d (%s)", si->si_code, ed.s_name);
1246 
1247   if (si->si_errno) {
1248     os->print(", si_errno: %d", si->si_errno);
1249   }
1250 
1251   // Output additional information depending on the signal code.
1252 
1253   // Note: Many implementations lump si_addr, si_pid, si_uid etc. together as unions,
1254   // so it depends on the context which member to use. For synchronous error signals,
1255   // we print si_addr, unless the signal was sent by another process or thread, in
1256   // which case we print out pid or tid of the sender.
1257   if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
1258     const pid_t pid = si->si_pid;
1259     os->print(", si_pid: %ld", (long) pid);
1260     if (IS_VALID_PID(pid)) {
1261       const pid_t me = getpid();
1262       if (me == pid) {
1263         os->print(" (current process)");
1264       }
1265     } else {
1266       os->print(" (invalid)");
1267     }
1268     os->print(", si_uid: %ld", (long) si->si_uid);
1269     if (sig == SIGCHLD) {
1270       os->print(", si_status: %d", si->si_status);
1271     }
1272   } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
1273              sig == SIGTRAP || sig == SIGFPE) {
1274     os->print(", si_addr: " PTR_FORMAT, p2i(si->si_addr));
1275 #ifdef SIGPOLL
1276   } else if (sig == SIGPOLL) {
1277     os->print(", si_band: %ld", si->si_band);
1278 #endif
1279   }
1280 
1281 }
1282 
1283 int os::Posix::unblock_thread_signal_mask(const sigset_t *set) {
1284   return pthread_sigmask(SIG_UNBLOCK, set, NULL);
1285 }
1286 
1287 address os::Posix::ucontext_get_pc(const ucontext_t* ctx) {
1288 #if defined(AIX)
1289    return Aix::ucontext_get_pc(ctx);
1290 #elif defined(BSD)
1291    return Bsd::ucontext_get_pc(ctx);
1292 #elif defined(LINUX)
1293    return Linux::ucontext_get_pc(ctx);
1294 #elif defined(SOLARIS)
1295    return Solaris::ucontext_get_pc(ctx);
1296 #else
1297    VMError::report_and_die("unimplemented ucontext_get_pc");
1298 #endif
1299 }
1300 
1301 void os::Posix::ucontext_set_pc(ucontext_t* ctx, address pc) {
1302 #if defined(AIX)
1303    Aix::ucontext_set_pc(ctx, pc);
1304 #elif defined(BSD)
1305    Bsd::ucontext_set_pc(ctx, pc);
1306 #elif defined(LINUX)
1307    Linux::ucontext_set_pc(ctx, pc);
1308 #elif defined(SOLARIS)
1309    Solaris::ucontext_set_pc(ctx, pc);
1310 #else
1311    VMError::report_and_die("unimplemented ucontext_get_pc");
1312 #endif
1313 }
1314 
1315 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {
1316   size_t stack_size = 0;
1317   size_t guard_size = 0;
1318   int detachstate = 0;
1319   pthread_attr_getstacksize(attr, &stack_size);
1320   pthread_attr_getguardsize(attr, &guard_size);
1321   // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.
1322   LINUX_ONLY(stack_size -= guard_size);
1323   pthread_attr_getdetachstate(attr, &detachstate);
1324   jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",
1325     stack_size / 1024, guard_size / 1024,
1326     (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));
1327   return buf;
1328 }
1329 
1330 char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {
1331 
1332   if (filename == NULL || outbuf == NULL || outbuflen < 1) {
1333     assert(false, "os::Posix::realpath: invalid arguments.");
1334     errno = EINVAL;
1335     return NULL;
1336   }
1337 
1338   char* result = NULL;
1339 
1340   // This assumes platform realpath() is implemented according to POSIX.1-2008.
1341   // POSIX.1-2008 allows to specify NULL for the output buffer, in which case
1342   // output buffer is dynamically allocated and must be ::free()'d by the caller.
1343   char* p = ::realpath(filename, NULL);
1344   if (p != NULL) {
1345     if (strlen(p) < outbuflen) {
1346       strcpy(outbuf, p);
1347       result = outbuf;
1348     } else {
1349       errno = ENAMETOOLONG;
1350     }
1351     ::free(p); // *not* os::free
1352   } else {
1353     // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath
1354     // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and
1355     // that it complains about the NULL we handed down as user buffer.
1356     // In this case, use the user provided buffer but at least check whether realpath caused
1357     // a memory overwrite.
1358     if (errno == EINVAL) {
1359       outbuf[outbuflen - 1] = '\0';
1360       p = ::realpath(filename, outbuf);
1361       if (p != NULL) {
1362         guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");
1363         result = p;
1364       }
1365     }
1366   }
1367   return result;
1368 
1369 }
1370 
1371 int os::stat(const char *path, struct stat *sbuf) {
1372   return ::stat(path, sbuf);
1373 }
1374 
1375 char * os::native_path(char *path) {
1376   return path;
1377 }
1378 
1379 // Check minimum allowable stack sizes for thread creation and to initialize
1380 // the java system classes, including StackOverflowError - depends on page
1381 // size.
1382 // The space needed for frames during startup is platform dependent. It
1383 // depends on word size, platform calling conventions, C frame layout and
1384 // interpreter/C1/C2 design decisions. Therefore this is given in a
1385 // platform (os/cpu) dependent constant.
1386 // To this, space for guard mechanisms is added, which depends on the
1387 // page size which again depends on the concrete system the VM is running
1388 // on. Space for libc guard pages is not included in this size.
1389 jint os::Posix::set_minimum_stack_sizes() {
1390   size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1391 
1392   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1393                                    JavaThread::stack_guard_zone_size() +
1394                                    JavaThread::stack_shadow_zone_size();
1395 
1396   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
1397   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1398 
1399   size_t stack_size_in_bytes = ThreadStackSize * K;
1400   if (stack_size_in_bytes != 0 &&
1401       stack_size_in_bytes < _java_thread_min_stack_allowed) {
1402     // The '-Xss' and '-XX:ThreadStackSize=N' options both set
1403     // ThreadStackSize so we go with "Java thread stack size" instead
1404     // of "ThreadStackSize" to be more friendly.
1405     tty->print_cr("\nThe Java thread stack size specified is too small. "
1406                   "Specify at least " SIZE_FORMAT "k",
1407                   _java_thread_min_stack_allowed / K);
1408     return JNI_ERR;
1409   }
1410 
1411   // Make the stack size a multiple of the page size so that
1412   // the yellow/red zones can be guarded.
1413   JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));
1414 
1415   // Reminder: a compiler thread is a Java thread.
1416   _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
1417                                        JavaThread::stack_guard_zone_size() +
1418                                        JavaThread::stack_shadow_zone_size();
1419 
1420   _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());
1421   _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);
1422 
1423   stack_size_in_bytes = CompilerThreadStackSize * K;
1424   if (stack_size_in_bytes != 0 &&
1425       stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
1426     tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
1427                   "Specify at least " SIZE_FORMAT "k",
1428                   _compiler_thread_min_stack_allowed / K);
1429     return JNI_ERR;
1430   }
1431 
1432   _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
1433   _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);
1434 
1435   stack_size_in_bytes = VMThreadStackSize * K;
1436   if (stack_size_in_bytes != 0 &&
1437       stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
1438     tty->print_cr("\nThe VMThreadStackSize specified is too small. "
1439                   "Specify at least " SIZE_FORMAT "k",
1440                   _vm_internal_thread_min_stack_allowed / K);
1441     return JNI_ERR;
1442   }
1443   return JNI_OK;
1444 }
1445 
1446 // Called when creating the thread.  The minimum stack sizes have already been calculated
1447 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
1448   size_t stack_size;
1449   if (req_stack_size == 0) {
1450     stack_size = default_stack_size(thr_type);
1451   } else {
1452     stack_size = req_stack_size;
1453   }
1454 
1455   switch (thr_type) {
1456   case os::java_thread:
1457     // Java threads use ThreadStackSize which default value can be
1458     // changed with the flag -Xss
1459     if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) {
1460       // no requested size and we have a more specific default value
1461       stack_size = JavaThread::stack_size_at_create();
1462     }
1463     stack_size = MAX2(stack_size,
1464                       _java_thread_min_stack_allowed);
1465     break;
1466   case os::compiler_thread:
1467     if (req_stack_size == 0 && CompilerThreadStackSize > 0) {
1468       // no requested size and we have a more specific default value
1469       stack_size = (size_t)(CompilerThreadStackSize * K);
1470     }
1471     stack_size = MAX2(stack_size,
1472                       _compiler_thread_min_stack_allowed);
1473     break;
1474   case os::vm_thread:
1475   case os::pgc_thread:
1476   case os::cgc_thread:
1477   case os::watcher_thread:
1478   default:  // presume the unknown thr_type is a VM internal
1479     if (req_stack_size == 0 && VMThreadStackSize > 0) {
1480       // no requested size and we have a more specific default value
1481       stack_size = (size_t)(VMThreadStackSize * K);
1482     }
1483 
1484     stack_size = MAX2(stack_size,
1485                       _vm_internal_thread_min_stack_allowed);
1486     break;
1487   }
1488 
1489   // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.
1490   // Be careful not to round up to 0. Align down in that case.
1491   if (stack_size <= SIZE_MAX - vm_page_size()) {
1492     stack_size = align_up(stack_size, vm_page_size());
1493   } else {
1494     stack_size = align_down(stack_size, vm_page_size());
1495   }
1496 
1497   return stack_size;
1498 }
1499 
1500 bool os::Posix::is_root(uid_t uid){
1501     return ROOT_UID == uid;
1502 }
1503 
1504 bool os::Posix::matches_effective_uid_or_root(uid_t uid) {
1505     return is_root(uid) || geteuid() == uid;
1506 }
1507 
1508 bool os::Posix::matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid) {
1509     return is_root(uid) || (geteuid() == uid && getegid() == gid);
1510 }
1511 
1512 Thread* os::ThreadCrashProtection::_protected_thread = NULL;
1513 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;
1514 volatile intptr_t os::ThreadCrashProtection::_crash_mux = 0;
1515 
1516 os::ThreadCrashProtection::ThreadCrashProtection() {
1517 }
1518 
1519 /*
1520  * See the caveats for this class in os_posix.hpp
1521  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
1522  * method and returns false. If none of the signals are raised, returns true.
1523  * The callback is supposed to provide the method that should be protected.
1524  */
1525 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
1526   sigset_t saved_sig_mask;
1527 
1528   Thread::muxAcquire(&_crash_mux, "CrashProtection");
1529 
1530   _protected_thread = Thread::current_or_null();
1531   assert(_protected_thread != NULL, "Cannot crash protect a NULL thread");
1532 
1533   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
1534   // since on at least some systems (OS X) siglongjmp will restore the mask
1535   // for the process, not the thread
1536   pthread_sigmask(0, NULL, &saved_sig_mask);
1537   if (sigsetjmp(_jmpbuf, 0) == 0) {
1538     // make sure we can see in the signal handler that we have crash protection
1539     // installed
1540     _crash_protection = this;
1541     cb.call();
1542     // and clear the crash protection
1543     _crash_protection = NULL;
1544     _protected_thread = NULL;
1545     Thread::muxRelease(&_crash_mux);
1546     return true;
1547   }
1548   // this happens when we siglongjmp() back
1549   pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
1550   _crash_protection = NULL;
1551   _protected_thread = NULL;
1552   Thread::muxRelease(&_crash_mux);
1553   return false;
1554 }
1555 
1556 void os::ThreadCrashProtection::restore() {
1557   assert(_crash_protection != NULL, "must have crash protection");
1558   siglongjmp(_jmpbuf, 1);
1559 }
1560 
1561 void os::ThreadCrashProtection::check_crash_protection(int sig,
1562     Thread* thread) {
1563 
1564   if (thread != NULL &&
1565       thread == _protected_thread &&
1566       _crash_protection != NULL) {
1567 
1568     if (sig == SIGSEGV || sig == SIGBUS) {
1569       _crash_protection->restore();
1570     }
1571   }
1572 }
1573 
1574 
1575 // Shared pthread_mutex/cond based PlatformEvent implementation.
1576 // Not currently usable by Solaris.
1577 
1578 #ifndef SOLARIS
1579 
1580 // Shared condattr object for use with relative timed-waits. Will be associated
1581 // with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes,
1582 // but otherwise whatever default is used by the platform - generally the
1583 // time-of-day clock.
1584 static pthread_condattr_t _condAttr[1];
1585 
1586 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not
1587 // all systems (e.g. FreeBSD) map the default to "normal".
1588 static pthread_mutexattr_t _mutexAttr[1];
1589 
1590 // common basic initialization that is always supported
1591 static void pthread_init_common(void) {
1592   int status;
1593   if ((status = pthread_condattr_init(_condAttr)) != 0) {
1594     fatal("pthread_condattr_init: %s", os::strerror(status));
1595   }
1596   if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {
1597     fatal("pthread_mutexattr_init: %s", os::strerror(status));
1598   }
1599   if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {
1600     fatal("pthread_mutexattr_settype: %s", os::strerror(status));
1601   }
1602 }
1603 
1604 // Not all POSIX types and API's are available on all notionally "posix"
1605 // platforms. If we have build-time support then we will check for actual
1606 // runtime support via dlopen/dlsym lookup. This allows for running on an
1607 // older OS version compared to the build platform. But if there is no
1608 // build time support then there cannot be any runtime support as we do not
1609 // know what the runtime types would be (for example clockid_t might be an
1610 // int or int64_t).
1611 //
1612 #ifdef SUPPORTS_CLOCK_MONOTONIC
1613 
1614 // This means we have clockid_t, clock_gettime et al and CLOCK_MONOTONIC
1615 
1616 static int (*_clock_gettime)(clockid_t, struct timespec *);
1617 static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t);
1618 
1619 static bool _use_clock_monotonic_condattr;
1620 
1621 // Determine what POSIX API's are present and do appropriate
1622 // configuration.
1623 void os::Posix::init(void) {
1624 
1625   // NOTE: no logging available when this is called. Put logging
1626   // statements in init_2().
1627 
1628   // Copied from os::Linux::clock_init(). The duplication is temporary.
1629 
1630   // 1. Check for CLOCK_MONOTONIC support.
1631 
1632   void* handle = NULL;
1633 
1634   // For linux we need librt, for other OS we can find
1635   // this function in regular libc.
1636 #ifdef NEEDS_LIBRT
1637   // We do dlopen's in this particular order due to bug in linux
1638   // dynamic loader (see 6348968) leading to crash on exit.
1639   handle = dlopen("librt.so.1", RTLD_LAZY);
1640   if (handle == NULL) {
1641     handle = dlopen("librt.so", RTLD_LAZY);
1642   }
1643 #endif
1644 
1645   if (handle == NULL) {
1646     handle = RTLD_DEFAULT;
1647   }
1648 
1649   _clock_gettime = NULL;
1650 
1651   int (*clock_getres_func)(clockid_t, struct timespec*) =
1652     (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
1653   int (*clock_gettime_func)(clockid_t, struct timespec*) =
1654     (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
1655   if (clock_getres_func != NULL && clock_gettime_func != NULL) {
1656     // We assume that if both clock_gettime and clock_getres support
1657     // CLOCK_MONOTONIC then the OS provides true high-res monotonic clock.
1658     struct timespec res;
1659     struct timespec tp;
1660     if (clock_getres_func(CLOCK_MONOTONIC, &res) == 0 &&
1661         clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
1662       // Yes, monotonic clock is supported.
1663       _clock_gettime = clock_gettime_func;
1664     } else {
1665 #ifdef NEEDS_LIBRT
1666       // Close librt if there is no monotonic clock.
1667       if (handle != RTLD_DEFAULT) {
1668         dlclose(handle);
1669       }
1670 #endif
1671     }
1672   }
1673 
1674   // 2. Check for pthread_condattr_setclock support.
1675 
1676   _pthread_condattr_setclock = NULL;
1677 
1678   // libpthread is already loaded.
1679   int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) =
1680     (int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT,
1681                                                    "pthread_condattr_setclock");
1682   if (condattr_setclock_func != NULL) {
1683     _pthread_condattr_setclock = condattr_setclock_func;
1684   }
1685 
1686   // Now do general initialization.
1687 
1688   pthread_init_common();
1689 
1690   int status;
1691   if (_pthread_condattr_setclock != NULL && _clock_gettime != NULL) {
1692     if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) {
1693       if (status == EINVAL) {
1694         _use_clock_monotonic_condattr = false;
1695         warning("Unable to use monotonic clock with relative timed-waits" \
1696                 " - changes to the time-of-day clock may have adverse affects");
1697       } else {
1698         fatal("pthread_condattr_setclock: %s", os::strerror(status));
1699       }
1700     } else {
1701       _use_clock_monotonic_condattr = true;
1702     }
1703   } else {
1704     _use_clock_monotonic_condattr = false;
1705   }
1706 }
1707 
1708 void os::Posix::init_2(void) {
1709   log_info(os)("Use of CLOCK_MONOTONIC is%s supported",
1710                (_clock_gettime != NULL ? "" : " not"));
1711   log_info(os)("Use of pthread_condattr_setclock is%s supported",
1712                (_pthread_condattr_setclock != NULL ? "" : " not"));
1713   log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s",
1714                _use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock");
1715 }
1716 
1717 #else // !SUPPORTS_CLOCK_MONOTONIC
1718 
1719 void os::Posix::init(void) {
1720   pthread_init_common();
1721 }
1722 
1723 void os::Posix::init_2(void) {
1724   log_info(os)("Use of CLOCK_MONOTONIC is not supported");
1725   log_info(os)("Use of pthread_condattr_setclock is not supported");
1726   log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with the default clock");
1727 }
1728 
1729 #endif // SUPPORTS_CLOCK_MONOTONIC
1730 
1731 os::PlatformEvent::PlatformEvent() {
1732   int status = pthread_cond_init(_cond, _condAttr);
1733   assert_status(status == 0, status, "cond_init");
1734   status = pthread_mutex_init(_mutex, _mutexAttr);
1735   assert_status(status == 0, status, "mutex_init");
1736   _event   = 0;
1737   _nParked = 0;
1738 }
1739 
1740 // Utility to convert the given timeout to an absolute timespec
1741 // (based on the appropriate clock) to use with pthread_cond_timewait.
1742 // The clock queried here must be the clock used to manage the
1743 // timeout of the condition variable.
1744 //
1745 // The passed in timeout value is either a relative time in nanoseconds
1746 // or an absolute time in milliseconds. A relative timeout will be
1747 // associated with CLOCK_MONOTONIC if available; otherwise, or if absolute,
1748 // the default time-of-day clock will be used.
1749 
1750 // Given time is a 64-bit value and the time_t used in the timespec is
1751 // sometimes a signed-32-bit value we have to watch for overflow if times
1752 // way in the future are given. Further on Solaris versions
1753 // prior to 10 there is a restriction (see cond_timedwait) that the specified
1754 // number of seconds, in abstime, is less than current_time + 100000000.
1755 // As it will be over 20 years before "now + 100000000" will overflow we can
1756 // ignore overflow and just impose a hard-limit on seconds using the value
1757 // of "now + 100000000". This places a limit on the timeout of about 3.17
1758 // years from "now".
1759 //
1760 #define MAX_SECS 100000000
1761 
1762 // Calculate a new absolute time that is "timeout" nanoseconds from "now".
1763 // "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending
1764 // on which clock is being used).
1765 static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec,
1766                           jlong now_part_sec, jlong unit) {
1767   time_t max_secs = now_sec + MAX_SECS;
1768 
1769   jlong seconds = timeout / NANOUNITS;
1770   timeout %= NANOUNITS; // remaining nanos
1771 
1772   if (seconds >= MAX_SECS) {
1773     // More seconds than we can add, so pin to max_secs.
1774     abstime->tv_sec = max_secs;
1775     abstime->tv_nsec = 0;
1776   } else {
1777     abstime->tv_sec = now_sec  + seconds;
1778     long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout;
1779     if (nanos >= NANOUNITS) { // overflow
1780       abstime->tv_sec += 1;
1781       nanos -= NANOUNITS;
1782     }
1783     abstime->tv_nsec = nanos;
1784   }
1785 }
1786 
1787 // Unpack the given deadline in milliseconds since the epoch, into the given timespec.
1788 // The current time in seconds is also passed in to enforce an upper bound as discussed above.
1789 static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) {
1790   time_t max_secs = now_sec + MAX_SECS;
1791 
1792   jlong seconds = deadline / MILLIUNITS;
1793   jlong millis = deadline % MILLIUNITS;
1794 
1795   if (seconds >= max_secs) {
1796     // Absolute seconds exceeds allowed max, so pin to max_secs.
1797     abstime->tv_sec = max_secs;
1798     abstime->tv_nsec = 0;
1799   } else {
1800     abstime->tv_sec = seconds;
1801     abstime->tv_nsec = millis * (NANOUNITS / MILLIUNITS);
1802   }
1803 }
1804 
1805 static void to_abstime(timespec* abstime, jlong timeout, bool isAbsolute) {
1806   DEBUG_ONLY(int max_secs = MAX_SECS;)
1807 
1808   if (timeout < 0) {
1809     timeout = 0;
1810   }
1811 
1812 #ifdef SUPPORTS_CLOCK_MONOTONIC
1813 
1814   if (_use_clock_monotonic_condattr && !isAbsolute) {
1815     struct timespec now;
1816     int status = _clock_gettime(CLOCK_MONOTONIC, &now);
1817     assert_status(status == 0, status, "clock_gettime");
1818     calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS);
1819     DEBUG_ONLY(max_secs += now.tv_sec;)
1820   } else {
1821 
1822 #else
1823 
1824   { // Match the block scope.
1825 
1826 #endif // SUPPORTS_CLOCK_MONOTONIC
1827 
1828     // Time-of-day clock is all we can reliably use.
1829     struct timeval now;
1830     int status = gettimeofday(&now, NULL);
1831     assert_status(status == 0, errno, "gettimeofday");
1832     if (isAbsolute) {
1833       unpack_abs_time(abstime, timeout, now.tv_sec);
1834     } else {
1835       calc_rel_time(abstime, timeout, now.tv_sec, now.tv_usec, MICROUNITS);
1836     }
1837     DEBUG_ONLY(max_secs += now.tv_sec;)
1838   }
1839 
1840   assert(abstime->tv_sec >= 0, "tv_sec < 0");
1841   assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs");
1842   assert(abstime->tv_nsec >= 0, "tv_nsec < 0");
1843   assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS");
1844 }
1845 
1846 // PlatformEvent
1847 //
1848 // Assumption:
1849 //    Only one parker can exist on an event, which is why we allocate
1850 //    them per-thread. Multiple unparkers can coexist.
1851 //
1852 // _event serves as a restricted-range semaphore.
1853 //   -1 : thread is blocked, i.e. there is a waiter
1854 //    0 : neutral: thread is running or ready,
1855 //        could have been signaled after a wait started
1856 //    1 : signaled - thread is running or ready
1857 //
1858 //    Having three states allows for some detection of bad usage - see
1859 //    comments on unpark().
1860 
1861 void os::PlatformEvent::park() {       // AKA "down()"
1862   // Transitions for _event:
1863   //   -1 => -1 : illegal
1864   //    1 =>  0 : pass - return immediately
1865   //    0 => -1 : block; then set _event to 0 before returning
1866 
1867   // Invariant: Only the thread associated with the PlatformEvent
1868   // may call park().
1869   assert(_nParked == 0, "invariant");
1870 
1871   int v;
1872 
1873   // atomically decrement _event
1874   for (;;) {
1875     v = _event;
1876     if (Atomic::cmpxchg(v - 1, &_event, v) == v) break;
1877   }
1878   guarantee(v >= 0, "invariant");
1879 
1880   if (v == 0) { // Do this the hard way by blocking ...
1881     int status = pthread_mutex_lock(_mutex);
1882     assert_status(status == 0, status, "mutex_lock");
1883     guarantee(_nParked == 0, "invariant");
1884     ++_nParked;
1885     while (_event < 0) {
1886       // OS-level "spurious wakeups" are ignored
1887       status = pthread_cond_wait(_cond, _mutex);
1888       assert_status(status == 0, status, "cond_wait");
1889     }
1890     --_nParked;
1891 
1892     _event = 0;
1893     status = pthread_mutex_unlock(_mutex);
1894     assert_status(status == 0, status, "mutex_unlock");
1895     // Paranoia to ensure our locked and lock-free paths interact
1896     // correctly with each other.
1897     OrderAccess::fence();
1898   }
1899   guarantee(_event >= 0, "invariant");
1900 }
1901 
1902 int os::PlatformEvent::park(jlong millis) {
1903   // Transitions for _event:
1904   //   -1 => -1 : illegal
1905   //    1 =>  0 : pass - return immediately
1906   //    0 => -1 : block; then set _event to 0 before returning
1907 
1908   // Invariant: Only the thread associated with the Event/PlatformEvent
1909   // may call park().
1910   assert(_nParked == 0, "invariant");
1911 
1912   int v;
1913   // atomically decrement _event
1914   for (;;) {
1915     v = _event;
1916     if (Atomic::cmpxchg(v - 1, &_event, v) == v) break;
1917   }
1918   guarantee(v >= 0, "invariant");
1919 
1920   if (v == 0) { // Do this the hard way by blocking ...
1921     struct timespec abst;
1922     // We have to watch for overflow when converting millis to nanos,
1923     // but if millis is that large then we will end up limiting to
1924     // MAX_SECS anyway, so just do that here.
1925     if (millis / MILLIUNITS > MAX_SECS) {
1926       millis = jlong(MAX_SECS) * MILLIUNITS;
1927     }
1928     to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false);
1929 
1930     int ret = OS_TIMEOUT;
1931     int status = pthread_mutex_lock(_mutex);
1932     assert_status(status == 0, status, "mutex_lock");
1933     guarantee(_nParked == 0, "invariant");
1934     ++_nParked;
1935 
1936     while (_event < 0) {
1937       status = pthread_cond_timedwait(_cond, _mutex, &abst);
1938       assert_status(status == 0 || status == ETIMEDOUT,
1939                     status, "cond_timedwait");
1940       // OS-level "spurious wakeups" are ignored unless the archaic
1941       // FilterSpuriousWakeups is set false. That flag should be obsoleted.
1942       if (!FilterSpuriousWakeups) break;
1943       if (status == ETIMEDOUT) break;
1944     }
1945     --_nParked;
1946 
1947     if (_event >= 0) {
1948       ret = OS_OK;
1949     }
1950 
1951     _event = 0;
1952     status = pthread_mutex_unlock(_mutex);
1953     assert_status(status == 0, status, "mutex_unlock");
1954     // Paranoia to ensure our locked and lock-free paths interact
1955     // correctly with each other.
1956     OrderAccess::fence();
1957     return ret;
1958   }
1959   return OS_OK;
1960 }
1961 
1962 void os::PlatformEvent::unpark() {
1963   // Transitions for _event:
1964   //    0 => 1 : just return
1965   //    1 => 1 : just return
1966   //   -1 => either 0 or 1; must signal target thread
1967   //         That is, we can safely transition _event from -1 to either
1968   //         0 or 1.
1969   // See also: "Semaphores in Plan 9" by Mullender & Cox
1970   //
1971   // Note: Forcing a transition from "-1" to "1" on an unpark() means
1972   // that it will take two back-to-back park() calls for the owning
1973   // thread to block. This has the benefit of forcing a spurious return
1974   // from the first park() call after an unpark() call which will help
1975   // shake out uses of park() and unpark() without checking state conditions
1976   // properly. This spurious return doesn't manifest itself in any user code
1977   // but only in the correctly written condition checking loops of ObjectMonitor,
1978   // Mutex/Monitor, Thread::muxAcquire and os::sleep
1979 
1980   if (Atomic::xchg(1, &_event) >= 0) return;
1981 
1982   int status = pthread_mutex_lock(_mutex);
1983   assert_status(status == 0, status, "mutex_lock");
1984   int anyWaiters = _nParked;
1985   assert(anyWaiters == 0 || anyWaiters == 1, "invariant");
1986   status = pthread_mutex_unlock(_mutex);
1987   assert_status(status == 0, status, "mutex_unlock");
1988 
1989   // Note that we signal() *after* dropping the lock for "immortal" Events.
1990   // This is safe and avoids a common class of futile wakeups.  In rare
1991   // circumstances this can cause a thread to return prematurely from
1992   // cond_{timed}wait() but the spurious wakeup is benign and the victim
1993   // will simply re-test the condition and re-park itself.
1994   // This provides particular benefit if the underlying platform does not
1995   // provide wait morphing.
1996 
1997   if (anyWaiters != 0) {
1998     status = pthread_cond_signal(_cond);
1999     assert_status(status == 0, status, "cond_signal");
2000   }
2001 }
2002 
2003 // JSR166 support
2004 
2005  os::PlatformParker::PlatformParker() {
2006   int status;
2007   status = pthread_cond_init(&_cond[REL_INDEX], _condAttr);
2008   assert_status(status == 0, status, "cond_init rel");
2009   status = pthread_cond_init(&_cond[ABS_INDEX], NULL);
2010   assert_status(status == 0, status, "cond_init abs");
2011   status = pthread_mutex_init(_mutex, _mutexAttr);
2012   assert_status(status == 0, status, "mutex_init");
2013   _cur_index = -1; // mark as unused
2014 }
2015 
2016 // Parker::park decrements count if > 0, else does a condvar wait.  Unpark
2017 // sets count to 1 and signals condvar.  Only one thread ever waits
2018 // on the condvar. Contention seen when trying to park implies that someone
2019 // is unparking you, so don't wait. And spurious returns are fine, so there
2020 // is no need to track notifications.
2021 
2022 void Parker::park(bool isAbsolute, jlong time) {
2023 
2024   // Optional fast-path check:
2025   // Return immediately if a permit is available.
2026   // We depend on Atomic::xchg() having full barrier semantics
2027   // since we are doing a lock-free update to _counter.
2028   if (Atomic::xchg(0, &_counter) > 0) return;
2029 
2030   Thread* thread = Thread::current();
2031   assert(thread->is_Java_thread(), "Must be JavaThread");
2032   JavaThread *jt = (JavaThread *)thread;
2033 
2034   // Optional optimization -- avoid state transitions if there's
2035   // an interrupt pending.
2036   if (Thread::is_interrupted(thread, false)) {
2037     return;
2038   }
2039 
2040   // Next, demultiplex/decode time arguments
2041   struct timespec absTime;
2042   if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all
2043     return;
2044   }
2045   if (time > 0) {
2046     to_abstime(&absTime, time, isAbsolute);
2047   }
2048 
2049   // Enter safepoint region
2050   // Beware of deadlocks such as 6317397.
2051   // The per-thread Parker:: mutex is a classic leaf-lock.
2052   // In particular a thread must never block on the Threads_lock while
2053   // holding the Parker:: mutex.  If safepoints are pending both the
2054   // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
2055   ThreadBlockInVM tbivm(jt);
2056 
2057   // Don't wait if cannot get lock since interference arises from
2058   // unparking. Also re-check interrupt before trying wait.
2059   if (Thread::is_interrupted(thread, false) ||
2060       pthread_mutex_trylock(_mutex) != 0) {
2061     return;
2062   }
2063 
2064   int status;
2065   if (_counter > 0)  { // no wait needed
2066     _counter = 0;
2067     status = pthread_mutex_unlock(_mutex);
2068     assert_status(status == 0, status, "invariant");
2069     // Paranoia to ensure our locked and lock-free paths interact
2070     // correctly with each other and Java-level accesses.
2071     OrderAccess::fence();
2072     return;
2073   }
2074 
2075   OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
2076   jt->set_suspend_equivalent();
2077   // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2078 
2079   assert(_cur_index == -1, "invariant");
2080   if (time == 0) {
2081     _cur_index = REL_INDEX; // arbitrary choice when not timed
2082     status = pthread_cond_wait(&_cond[_cur_index], _mutex);
2083     assert_status(status == 0, status, "cond_timedwait");
2084   }
2085   else {
2086     _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
2087     status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);
2088     assert_status(status == 0 || status == ETIMEDOUT,
2089                   status, "cond_timedwait");
2090   }
2091   _cur_index = -1;
2092 
2093   _counter = 0;
2094   status = pthread_mutex_unlock(_mutex);
2095   assert_status(status == 0, status, "invariant");
2096   // Paranoia to ensure our locked and lock-free paths interact
2097   // correctly with each other and Java-level accesses.
2098   OrderAccess::fence();
2099 
2100   // If externally suspended while waiting, re-suspend
2101   if (jt->handle_special_suspend_equivalent_condition()) {
2102     jt->java_suspend_self();
2103   }
2104 }
2105 
2106 void Parker::unpark() {
2107   int status = pthread_mutex_lock(_mutex);
2108   assert_status(status == 0, status, "invariant");
2109   const int s = _counter;
2110   _counter = 1;
2111   // must capture correct index before unlocking
2112   int index = _cur_index;
2113   status = pthread_mutex_unlock(_mutex);
2114   assert_status(status == 0, status, "invariant");
2115 
2116   // Note that we signal() *after* dropping the lock for "immortal" Events.
2117   // This is safe and avoids a common class of futile wakeups.  In rare
2118   // circumstances this can cause a thread to return prematurely from
2119   // cond_{timed}wait() but the spurious wakeup is benign and the victim
2120   // will simply re-test the condition and re-park itself.
2121   // This provides particular benefit if the underlying platform does not
2122   // provide wait morphing.
2123 
2124   if (s < 1 && index != -1) {
2125     // thread is definitely parked
2126     status = pthread_cond_signal(&_cond[index]);
2127     assert_status(status == 0, status, "invariant");
2128   }
2129 }
2130 
2131 
2132 #endif // !SOLARIS