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