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